使用 intel.xdk 进行 https 调用

Making https calls with intel.xdk

我们正在尝试调用使用 SSL 证书的 .NET REST api。我们已将服务器标题设置为允许所有(使用 CORS),当我们尝试使用以下代码通过 Web 浏览器访问它时效果很好,但是当我们尝试使用 Intel XDK 创建的应用程序时,我们得到的只是错误(代码下方图片所示):

function test1() {
    var self = this;

    self.ajax = function (uri, method, data)
    {
        alert('sending: ' + data);
        var request = {
            url: uri,
            type: method,
            contentType: "x-www-form-urlencoded",
            accepts: "application/json",
            cache: false,
            origin: 'localhost',
            data: data, 
            success: function (result)
            {
                console.info("WORKS!");
            },
            error: function (jqXHR) {
                console.log("Got a response");
                console.log(jqXHR);
                console.log("ajax error " + jqXHR.status);
            }
        };
        return $.ajax(request);
    };

    self.ajax(
            'https://localhost:44301/token',
            'POST',
            'grant_type=XXX&username=XXX&password=XXX'
            )
            .done(function (data) {
                alert('got data from auth');
                alert(data.access_token);
            });
}

function test2()
{
    $.ajax({
        url: "https://localhost:44301/token",
        type: "POST",
        jsonp: "callback",
        dataType: "jsonp",
        data: "grant_type=XXX&username=XXX&password=XXX",
        crossDomain: true, //Don't think this is required really...
        jsonpCallback: function (data)
        {
            console.log("JSON P CALLBACK!");
            console.info(data);
        },
        success: function (response) {
            console.log(response);
        }
    });
}

在 Intel XDK 项目中,我们使用 xhr.js 文件,这似乎是解决这个问题的通用解决方案,但我还没有看到任何人实际使用它与 SSL 证书结合使用。

问题似乎是一个简单的内容编码问题。添加 header 后,将内容编码设置为 UTF-8,它起作用了:

$.ajax({
    url: 'https://localhost:44302/api/ping',
    type: 'POST',
    beforeSend: function (request)
    {
        //This part
        request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    },
    dataType: "json",
    data: JSON.stringify("Hello")
})
.done(function (result)
{
    console.info(result);
});