www.webdeveloper.com
Results 1 to 14 of 14

Thread: Help: Why does XMLHttpRequest status always returns 0

  1. #1
    Join Date
    Mar 2013
    Posts
    6

    Help: Why does XMLHttpRequest status always returns 0

    Hi everyone,

    XMLHttpRequest status returns 0. I am not sure why. I use relative URL, and I am using localhost. The response page is also on the same folder. So it is in the same domain. The code is as under:-

    Code:
    function getLike(value)
    {
        var xhr;
        alert(value);
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xhr=new XMLHttpRequest();
            alert("XMLHttpRequest1");
        }
        else
        {// code for IE6, IE5
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
            alert("Microsoft.XMLHTTP1");
        }
        xhr.onreadystatechange=function()
        {
            alert(xhr.readyState);
            if (xhr.readyState==4)
            {
                alert(xhr.status);
                if (xhr.status==200)
                {
                    document.getElementById("responseDIV").innerHTML=xhr.responseText;
                } 
            }
        }
        //alert("onreadystatechange");
        var likePath = "/Moms/babyNames/";
        var likeFile = likePath + "likeBabyName.php" ;
        var likeURL = likeFile + '?name='+ value;
        alert(likeFile);
        xhr.open("GET",likeURL,true);
        xhr.send();
    }
    likeBabyName.php has only one line as:

    Code:
    <?php
    echo "LikedName";
    ?>
    Response status is 0. I have looked in web for answers, but have not found any solution for my problem. If you can point to me where I am going wrong, I would be helped very much.

    Thanks,
    Priya

  2. #2
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    Looking at your work it seems OK. The response status 0 may be due to the odditie of not having a valid URL. Check you final URL path to make sure it does actually point to you file. Put into you address bar of the browser to see if it opens the php file directly.

    The variables likePath, likeFile and likeURL should not cause a problem if done right but I suggest you not do that till you sort out the problem. All of it can be included into one string with the value added at the end, but better still will be to put it into the open method bypassing the variables altogether.

    I do not believe the alerts should cause any problems BUT please learn to use the browsers console.log feature to display all the information that you are trying to get from the alerts. You will be surprised what information you will find when you use it

  3. #3
    Join Date
    Mar 2013
    Posts
    6
    Thank you for replying. I tried putting the whole url and also checked out that my URL file does print out "LikedName".

    This is what I have now:

    Code:
    xhr.open("GET","http://localhost/Moms/babyNames/likeBabyName.php",true);
    xhr.send();
    I am still getting xhr.status as 0 . Please help.

  4. #4
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    OK - what I suggested you do was...

    All of it can be included into one string with the value added at the end, but better still will be to put it into the open method bypassing the variables altogether.
    What that means is a two step process:

    1: take this

    Code:
    var likePath = "/Moms/babyNames/";
     var likeFile = likePath + "likeBabyName.php" ;
     var likeURL = likeFile + '?name='+ value;
    turn it into this....

    Code:
    var likeURL = "/Moms/babyNames/likeBabyName.php?name=" + value;
    NOW use that into you open method as before....

    Code:
    xhr.open("GET",likeURL,true);
    2: take this

    Code:
    likeURL = "/Moms/babyNames/likeBabyName.php?name=" + value;
    and do this....

    Code:
    xhr.open("GET","/Moms/babyNames/likeBabyName.php?name=" + value, true);
    or this

    Code:
    xhr.open("GET","/Moms/babyNames/likeBabyName.php", true);
    maybe you do not need to slash in front of Moms

    I DO NOT KNOW you file structure so YOU have to determine if that path is correct...and that I believe is the cause of your error.... SO CHECK IT please...

  5. #5
    Join Date
    Mar 2013
    Posts
    6
    that is the correct path name. I have checked it so many times

    Still need more suggestions and help.

  6. #6
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    Quote Originally Posted by priyankabhar View Post
    that is the correct path name. I have checked it so many times
    ...
    if that is true then I have made an error by assuming you have everything in place for it to work...

    OK. How did you check it?
    I did suggest a way to check it back at the very start.



    Describe your setup that you are using... How are you using the localhost and php?

  7. #7
    Join Date
    Mar 2009
    Posts
    492
    Usually a status code of zero means the request failed at the very beginning. Maybe I've not understood your set up, but if the original page that is trying to access the program on your local server did not come from the server, but was invoked as, for example, "file:///whatever/whatever/zap.html" it will be considered as a cross domain request which will fail.

    Is this the case?

  8. #8
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    Quote Originally Posted by Tcobb View Post
    ........
    Is this the case?
    after all what has happened i am afraid that may be the case...

    originally I did not want to think that but made the assumption anyway...

    no response to my last request so I better refrain from any accusations.

  9. #9
    Join Date
    Mar 2013
    Posts
    6
    I have

    Code:
    xhr.open("GET","http://localhost/Moms/babyNames/likeBabyName.php",true);
    xhr.send(null);
    So, the file is in the localhost server. And I am building/testing my website in localhost. How can it be cross-domain when the everything is in localhost?

    I have also checked that http://localhost/Moms/babyNames/likeBabyName.php does print "LikedName".

    Thank you for trying to help me, and I still need your help

  10. #10
    Join Date
    Mar 2009
    Posts
    492
    How do you activate the page that contains the javascript that makes the AJAX request? Is it also served up from the server? For example, does it start out because you've pointed your browser at "http://localhost/Moms/babyNames/index.html" ? If, on the other hand, you are activating the initial calling page by pointing your browser at the actual file, as in "file:///localhost/Moms/babyNames/index.html" this will indeed be interpreted as a cross domain request even though they are in the same directory.

    Once again-- how is the initial page that makes the AJAX call invoked?

  11. #11
    Join Date
    Mar 2013
    Posts
    6
    Thanks Tcobb for replying.

    The original file that calls the javascript file is : http://localhost/Moms/babyNames/babyBoyNames.php. It is also served from the server. This file has few babynames which I click to initiate the javascript function that makes the AJAX request to like the baby name. This file is invoked when a user clicks "babyBoyNames" menu item on the website in localhost server.

    Have I answered correctly?

    Thank you.

  12. #12
    Join Date
    Mar 2009
    Posts
    492
    Sorry--I have no idea what the problem may be. A status code of zero usually means that the network cannot be reached. Since the initial page loads I am somewhat mystified. Try this to see if it gives you any insight into what the error might be. Good luck.

    Code:
    function getLike(value){
        var xhr;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xhr=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xhr=new ActiveXObject("Microsoft.XMLHTTP");
        }
    
    xhr.open("GET", "/Moms/babyNames/likeBabyName.php", true);
    xhr.onload = function (e) {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          console.log(xhr.responseText);
        } else {
          console.error(xhr.statusText);
        }
      }
    };
    xhr.onerror = function (e) {
      console.error(xhr.statusText);
    };
    xhr.send(null);
    }

  13. #13
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    thanks Tcobb your question got the answer that I was after.


    if you tried

    Code:
    localhost/Moms/babyNames/likeBabyName.php
    and you got *likedName*

    then that path is working

    in post #4 I gave this example and comment...............

    Code:
    xhr.open("GET","/Moms/babyNames/likeBabyName.php", true);
    maybe you do not need to slash in front of Moms
    so now try

    Code:
    xhr.open("GET","Moms/babyNames/likeBabyName.php", true);
    have you used the error console to check for errors???

    Can you confirm if original javascript in post #1 is still being used


    ***A bit slow here BUT now matter ---two heads are better than one***
    Last edited by grumpyOleMan; 03-24-2013 at 12:29 AM. Reason: bit slow this end...

  14. #14
    Join Date
    Mar 2013
    Posts
    6
    Thanks grumpyOleMan and Tcobb. I was out not working for few days, so could not reply earlier.

    It seems Firefox is bit slow than Google Chrome...firefox was replying status 200 once in a while...So I looked and looked...and found this dumb mistake I had ...By the time Chrome was getting status = 200, my page was reloading itself because I had <a href = " " > in the page... I am not sure if I am explaining it right...I changed it to <a href="#">.

    Your replies and suggestions were so valuable to me...because of them I knew I was doing the Ajax part right, so I had to be really dumb somewhere else...thanks for putting up with me.

    Thanks again!!!

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 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