Search code examples
javascriptandroidhtmlcordovaintel-xdk

Intel XDK HTML5 App: http request always returns 0


I am working on a HTML5 app that needs to get data from a server. To do so I need to use Javascript, see below code. But no matter what I do, it always returns 0. It should, with the below code, return a 403 with correct url, or 404 with wrong url, it does neither. The container app should allow cross site scripting. I have the app running in the Intel App Preview. Windows 10 / Android 5.1.1 Sony Experia Z1 Compact.

What I want it to do is: connect to server, give server right username;password, return a package of data, to be used in App.

Tested these:
- Wifi works
- as does internet connection
- jQuery version of the below.

    var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://pageiuse.com", true);
    xhr.onload = function(){
    if (xhr.readyState == 4) {
        if(xhr.status == 200) {
            var json_string = xhr.responseText;
            var json = JSON.parse(json_string);
            toastMessage("Succes!");
        } else if(xhr.status == 404) {
            toastMessage("404!");
        } else {
            toastMessage("Error!!! " + xhr.status);
        }

    } else {
        toastMessage("readyState " + xhr.readyState);
    }};
    xhr.send(null);

All help is welcome, or suggestions on how to debug this.

*Update, there is data coming back when I use xhr.responseText, but xhr.status stays 0. Been digging into the "Cordova Whitelisting", but nothing getting better for now.


Solution

  • Here is how I solved it:

    @xmnboy provided the link to: use jQuery 2

    This is the code I used:

    $.ajax(
        {
        type: "GET",    
        crossDomain: true, //;paf; see http://stackoverflow.com/a/25109061/2914328
        url: "http://www.serverexample.com",
        async: true,
        dataType: "arraybuffer", // that is what I have incoming.
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Authorization", "basic username:password");
            }
        })
    
        .always(function (retorno, textStatus, jqXHR) { //;paf; see http://stackoverflow.com/a/19498463/2914328
            //toastMessage("jQuery version: " + $.fn.jquery + "\n retorno:" + retorno.status + "\n textStatus:" + textStatus + "\n jqXHR:" + jqXHR.status) ;
    
        var returnStatus = jqXHR.status ;
        if (returnStatus === undefined) {
            returnStatus = retorno.status;
        }
        switch (returnStatus) {
                case 0:
                    console.log("0 == exit oke", "Here") ;
    
                case 200:
                    console.log("200 == exit oke", "Here") ;
                    break;
    
                case 404:
                    console.log("404 == exit by fail", "Here") ;
                    break;
    
                default:
                    console.log("default switch happened") ;
                    console.log(JSON.stringify(jqXHR.responseJSON));
                    break ;
            }
    });