www.webdeveloper.com
Results 1 to 13 of 13

Thread: Status code 0

Hybrid View

  1. #1
    Join Date
    May 2011
    Posts
    12

    Status code 0

    Hi,

    I'm trying to get some data through a PHP file in AJAX. It works
    really fine, but only on Desktop. When i go mobile (iPhone 4, Android)
    it doesnt work. I've got a status = 0 back and my responseText is
    empty.

    It goes about the following code (with a callback).

    Code:
    function loadXML(method, url, onsuccess)
                            {
    
                            var xmlhttp;
    
                                    if (window.XMLHttpRequest)
                                    { // code for IE7+, Firefox, Chrome, Opera, Safari
                                      xmlhttp = new XMLHttpRequest();
                                    }
                                    else
                                    { // code for IE6, IE5
                                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                                    }
    
                                xmlhttp.onreadystatechange = function()
                                {
                                    if (xmlhttp.readyState == 4  && xmlhttp.status == 200) {
                                                    onsuccess(xmlhttp.responseText);
                                            }
    
                                            if(xmlhttp.readyState == 4  && xmlhttp.status == 0)
                                            {
                                                    onsuccess(xmlhttp.responseText);
                                            }
                                }
    
                                            xmlhttp.open(method, url, true);
                                            xmlhttp.send(null);
    
                            }
    
                            function initialize() {
    
                                    loadXML("GET", "http://example.com/testing.php",
    function(location) {
                                            //Here is the variabele
    location empty (only on Android/iPhone).
                                    });
                           }
    When i test my script on a browser, it gives me the right data back.
    On Android/iPhone, it gives nothing back. Knows anybody the problem
    and how i can solved it?

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    There something wrong within this part. There's a mess:
    Code:
    function initialize() {
    
                                    loadXML("GET", "http://example.com/testing.php",
    function(location) {
                                            //Here is the variabele
    location empty (only on Android/iPhone).
                                    });
                           }
    It is not very clear where the functions start and end...

  3. #3
    Join Date
    May 2011
    Posts
    12
    Maybe this version is better?

    Code:
    function loadXML(method, url, onsuccess)
    {
         var xmlhttp;
              if (window.XMLHttpRequest)
              { // code for IE7+, Firefox, Chrome, Opera, Safari
                      xmlhttp = new XMLHttpRequest();
              }
              else
              { // code for IE6, IE5
                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              }
    
              xmlhttp.onreadystatechange = function()
              {
                      if (xmlhttp.readyState == 4  && xmlhttp.status == 200) {
                           onsuccess(xmlhttp.responseText);
                      }
    
                      if(xmlhttp.readyState == 4  && xmlhttp.status == 0)
                      {
                           onsuccess(xmlhttp.responseText);
                      }
              }
    
              xmlhttp.open(method, url, true);
              xmlhttp.send(null);
    
    }
    
    function initialize() {
             loadXML("GET", "http://example.com/testing.php", function(location) {
                  //Here is the variabele location empty (only on Android/iPhone).
             });
    }

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Why so intricate? Wouldn't be easier something like this:
    Code:
    function loadXML(method, url){
    //...
    if (xmlhttp.readyState == 4  && xmlhttp.status == 200) {
    return xmlhttp.responseText);
    }
    //...
    }
    
    function initialize() {
    var location= loadXML("GET", "http://example.com/testing.php");
    // do whichever with location
    }

  5. #5
    Join Date
    May 2011
    Posts
    12
    Because of this function:

    Code:
    xmlhttp.onreadystatechange = function()
              {
                      if (xmlhttp.readyState == 4  && xmlhttp.status == 200) {
                           onsuccess(xmlhttp.responseText);
                      }
    
                      if(xmlhttp.readyState == 4  && xmlhttp.status == 0)
                      {
                           onsuccess(xmlhttp.responseText);
                      }
              }
    If i do a return, it would never come out the xmlhttp.onreadystatechange? That's why i'm using a callback function.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    status ==0? that means you are ready to work locally, if it is the case? If so, have you tried that?:
    Code:
    xmlhttp.onreadystatechange = function()
              {
                      if (xmlhttp.readyState == 4  && (xmlhttp.status == 200||xmlhttp.status == 0)) {
                           onsuccess(xmlhttp.responseText);
                      }
                      
              }

  7. #7
    Join Date
    May 2011
    Posts
    12
    Yes, i have tried that!

    The only problem is that i'm not working locally. I get that status code on my Android phone / iPhone 4.0.

    And if i do that. The xmlhttp.responseText is empty on my Android / iPhone 4.0

    Do you know what's wrong with it? I have searched already the whole web.

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Is it the url written correct? If you write it as an absolute URL (like http://www.etc) it won't work. Is it a relative path there?

  9. #9
    Join Date
    May 2011
    Posts
    12
    Quote Originally Posted by Kor View Post
    Is it the url written correct? If you write it as an absolute URL (like http://www.etc) it won't work. Is it a relative path there?
    That was the solution Thx!

  10. #10
    Join Date
    May 2011
    Posts
    12
    Thx, the problem has been solved!

  11. #11
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Leander View Post
    Thx, the problem has been solved!
    And the reason was? Please share the solution with us. To make us avoid a possible error, for the future

  12. #12
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    OK. The reason is simple: servers do often presume that a request to an absolute URL is a request to another domain, which is impossible without using a third, server-side, code (a proxy). the response status=0 usually cover that case.

  13. #13
    Join Date
    Jan 2013
    Posts
    1

    Status code 0

    would u plz specify wat relative link means?
    i have specified the url as http://ip addressortno/the web service name
    the web service is located on anoder machine so i have to specify the ip address

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles