http request always returns 0

http request always returns 0

I am stuck with an Intel XDK project: I want to get some data from a server, and no matter what I put in for url, it always returns a 0. And I have a hard time telling where it goes wrong. I am pretty sure the code is not the problem, but something else keeps it from reaching the server.

  • Wifi Works
  • So does the internet connection.
  • Rest of App does what I want it too
  • Plugins installed:
  • File Tranfer
  • Notification
  • Network Information
  • Tested, can get to external websites: used the inAppBrowser Plugin for that.

HTML5, CSS3, JavaScript, Intel XDK v3400, Debug on Android Sony Experia Z1 Compact running App Preview

Code: (I've used jQuery as well, same problem)

var xhr = new XMLHttpRequest();"GET", "", true);
    xhr.setRequestHeader("Authorization", "Basic: " + btoa(username + ":" + password)); // this should give acces.
    xhr.onload = function(){
        if (xhr.readyState == 4) {
            if(xhr.status == 200)
                var json_string = xhr.responseText;
                var json = JSON.parse(json_string);
            else if(xhr.status == 404)
        toastMessage("Error!!! " + xhr.status); // it always ends here...
    } else {
                toastMessage("readyState " + xhr.readyState);

*edit: added username / password inlog style.

5 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.


I see that the URL requires login, how are you handling this before the request? The server is prepared to use sessions after authentication?

I did a quick test with jquery, and get the same result as you, obviously I don't have an username and password.



Hello Diego,

thank you for testing. Even without a username / password you or I still should get an error. Been there, done that for a different Android app. So I know the server does what it should.



Working directly with the XmlHttpRequest object is fine in simple cases.  But if the connection is sophisticated, then it can be more challenging. I _like_ using the XmlHttpRequest object, but it's not always simple.


If you want simple, use jQuery.

$.ajax("someurl", {someoptions})
.then(function(data, xhr){


Here is how I solved it:

So this is (short version) what I went with:

    type: "GET",    
    crossDomain: true, //;paf; see
    url: "",
    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
        //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") ;

            case 404:
                console.log("404 == exit by fail", "Here") ;

                console.log("default switch happened") ;
                break ;


Leave a Comment

Please sign in to add a comment. Not a member? Join today