restsslintel-xdk

Making https calls with intel.xdk


We're trying to make a call to our .NET REST api which uses a SSL certificate. We have set the server headings to Allow All (using CORS) and it works great when we try to reach it via a web browser with the below code, but when we try with an application created with the Intel XDK all we get are errors (shown in pictures below the code):

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);
        }
    });
}

enter image description here enter image description here

In the Intel XDK project we use the xhr.js file which seems to be the universal solution to this problem every where but I haven't seen anyone actually use it in combination with SSL certificates.


Solution

  • The problem seems to have been a simple content encoding issue. After adding a header setting the content encoding to UTF-8 it worked:

    $.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);
    });