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();
    xhr.open("GET", "https://ewsupload.eljakim.nl/ppd", 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);
                toastMessage("Succes!");
            }
            else if(xhr.status == 404)
            {
                toastMessage("404!");
            }
            else
            {
        toastMessage("Error!!! " + xhr.status); // it always ends here...
    
        }
        
    } else {
                toastMessage("readyState " + xhr.readyState);
            }};
    xhr.send(null);

*edit: added username / password inlog style.

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

Hi,

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.

Regards,

Diego

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.

thanks!

Kees

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.  

 

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XM...

 

If you want simple, use jQuery.

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

 

Here is how I solved it:

https://software.intel.com/en-us/xdk/faqs/app-designer#ajax-jquery-one-fail

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

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

 

Leave a Comment

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