A notch above a monkey » XMLHttpRequest and status 0

XMLHttpRequest and status 0

Have you ever encountered a problem with XMLHttpRequest (XHR), where its handler couldn’t read request status or the status was set to 0?

Well, I did, a few days ago.

It took me a while to find the cause of this problem and in my case it was form submission. It’s stupid to use AJAX to submit a form, but nevertheless it’s what I’m doing for dubious reasons that I’ll probably be ashamed of in a few months or years. And it used to work fine, until I changed submit buttons from type=”button” to type=”image”.

First type needs javascript to do anything worthwhile, while second type submits form when pressed, which I somehow missed. So, when I thought I was sending only a normal XHR request, I was actually also submitting my form which lead to described strange behavior.

I have no idea why, since XHR state change handler shouldn’t be triggered by other requests, but it was and it was fixed by simply canceling form submission in image button onclick handler.

If you ever encounter this problem, check your forms and their submit buttons. It’s very likely that solution is hiding there.

Comments (32)

  1. [...] ng” rel=”tag” title=”View the technorati tag: Programming”> Jeremy Keith (via Marko Samastur) writes of a Hijax, a specific technique for graceful degradation: I wrote some time back ab [...]

  2. [...] jo have the code they do. To make it work. Marko Samastur wrote about a little quirk, and the magic of status code 0. Have you ever encountered a problem with XMLHttpRequest (XHR), where its handle [...]

  3. Assuming I read correctly here:

    You could also use an “onsubmit=return false” in the form element, preventing it from submitting at all. Also a convenient place to hook an XHR call for submitting, if you wanted.

  4. instead of putting the call to AJAX on the button did you try putting it on the form?

    Comment by redmyse — #
  5. <form onsubmit=”XHR(); return false;”>

    Comment by redmyse — #
  6. Thanks for comments and I’m sorry I wasn’t clear enough.

    The problem isn’t fixing this, when you know what you did wrong. As you all showed it’s quite easy and can be done in different ways.

    I wrote this post to help any misfortunate soul, who might make the same mistake I did and get puzzled by a strange error.

    Comment by markos #
  7. Marko Samastur, nel suo blog, affronta un particolare problema inerente all’oggetto XHR, ovvero quando lo statuscode è = 0 oppure lo status della request non viene correttamente letto dall’handler.

  8. I’ve tried to use XMLHttpRequest on Firefox vers 1.0.7 but it returns the status code set to 0. Here is my sample code:

    var isWorking = false;

    function handleHttpResponse() {
    if (http.readyState == 4) {
    if(http.status == 200) {
    var xmlDoc = http.responseXML;
    isWorking = false;
    } else {
    alert(“There was an error while retrieving the URL – status: ” + http.status);

    function tryxml() {
    if (!isWorking && http) {
    http.open(“GET”, “try.xml”, true);
    http.onreadystatechange = handleHttpResponse;
    isWorking = true;
    return false;

    function getHTTPObject() {
    var xmlhttp;

    if (!xmlhttp && typeof XMLHttpRequest != ‘undefined’) {
    try {
    xmlhttp = new XMLHttpRequest();
    } catch (e) {
    xmlhttp = false;

    return xmlhttp;

    var http = getHTTPObject(); // We create the HTTP Object


    And the try.xml file contains:

    John Smith

    Could you give me some hint?

    Comment by simona — #
  9. I see WordPress has stripped javascript again. Anyhow, it’s more or less the same problem.

    You have to prevent the default action of a link element. It doesn’t matter if it doesn’t do anything (void(0)). It simply mustn’t be executed.

    The simplest way to do so is to end your onclick handler with “return false”.

    Comment by markos #
  10. Hi,
    I’m facing a strange problem. When the link is a fixed URL (eg.: http://vidhan.blogspot.com), status is 200. Good.
    But when the link is a relative URL (eg.: home.html), status is returned as 0. Any ideas?
    I’m testing AJAX on my local hard-drive files. So the HTML file with AJAX script is index.html which wants another file ‘home.html’, but for the above problem.

    Comment by Vidhan #
  11. Hi all!
    the status code = 0 is due you are trying to make an ajax request in your local drive files (ie: file:///C:/MyTests/etc) but you should execute ajax in your local internet server (ie: http://local/MyTests/etc).


    Comment by jorgebg — #
  12. I am facing the same the same problem, I have the following HTML
    [form onsubmit="return false;"]
    [input type="button" id="btnCopyUsersID" value="Copy All" onclick="copy(); return false;"]
    [input type="button" id="btnCopySelectedUserID" disabled="true" value="Copy Selected" onclick="copy(true);return false;"]
    the copy method in javascript simply creates an AJAX request and submit a GET with other parameters. but I am still getting a status of 0 and empty responseText. This is working on other installations of IE and not working in other installations of IE, and there is no obvious difference in these installations. any ideas?

    Comment by tinashe — #
  13. Hi,
    If you all read first the W3C XMLHttpRequest activex doc. it sas clearly:
    ” uri of type DOMString
    A URI, which MUST be resolved to an absolute URI using the cached base URI.”

    The URI’s path should be always ABSOLUTE!


    Comment by Lolek — #
  14. Hi,

    I took a brief look in the XMLHTTPResponse Object specification. If you access certain elements of the XMLHTTPRequest object before readyState is in the proper state (for instance, if you try to access request.responseText before readyState is 3 or 4) an INVALID_STATE_ERR will be thrown. I think this the ’0′ in request.state.

    I do not know how this factors in to your problem, but it is something to think about when stepping through your code. I having the problem all over the place and have not quite isolated the error. Even more maddening is the fact that it seems to come and go. I’ll bounce back if I find anything.


    Comment by Mike #
  15. After reading further, I don’t think the INVALID_STATE_ERR and status code 0 are the same. I fought with this problem for a while, even copying code into my web app from other, working web apps. I finally switched from Apache 5.5 to Glassfish v1, and the problem disappeared.

    Comment by Mike #
  16. From Mozilla Developer Center:

    Note: file:/// and ftp:// do not return HTTP status, which is why they return zero for status and an empty string for statusText. Refer to bug 331610 for more insight.

    Comment by Simon #
  17. Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

    Comment by Alexwebmaster — #
  18. Thank you a lot for this post. It was helpful to me.

  19. Thank you. This helped me in fixing an issue.

    Comment by Harish — #
  20. Another reason for a status code of 0 is because of a cross domain conflict. I took your advice and disabled form submission with a return false, but I still got a status code of 0. I had a web page on my local drive and I was trying to send an XHR to a servlet running Tomcat at localhost but I got a status code of 0.

    The issue was resolved by allowing tomcat to host the web page. When they say that an XHR can only be made to the server from the same orgin as the web page, they mean it.

    See also:


    Comment by Jack Robinson — #
  21. hai friend, you are rocking. I had a problem for more than a week, within a minutes I did it.

    Comment by udham — #
  22. Dude, you are a lifesaver! Could’ve been banging my head for many more hours if I hadn’t come across your post.

    Comment by Igor G. — #
  23. hi,
    your clue about status code ’0′ was very useful,it saved my time
    thanks lot.
    from India

    Comment by Manjesh — #
  24. The answer to this problem is that the servlet that you are calling needs to set the Access-Control-Allow-Origin in the header, like this:


    For some reason, even in the same domain, anything based on Gecko, including Firefox, Chrome, etc. looks for this in the header from the servlet and if it doees not see this it aborts the connection prior to getting the xml from the servlet.

    Comment by mozandy #
  25. I was try to use AJAX to check connection to another page. It returned failed. I found that?
    open(“GET”, “www.google.ca”,true) //status return 0
    open(“GET”, “http://www.google.ca”,true) //status return 404.

    So I think the problem maybe in this URL.

    Comment by Jacky Liu — #
  26. I have an issue at this lines: Does anyone know why isn’t this command correct and where i’m wrong ? cause i’m facing the following error:

    “Could not open http protocol”

    http.open(‘GET’, ‘try.xml’, true);
    http.onreadystatechange = handleHttpResponse;

    Comment by Tim #
  27. me also facing the problem in xmlhttp.status=0 in firefox and chrome..although its working in IE…
    and so that xmlhttp.responseText is empty in firefox and chrome..

    Comment by Shailendra Sajwan — #
  28. Thank you. I spent several hours on it verifying hard-code, and the problem was simpler.

    Comment by Wesley Hilebrand — #
  29. I used button type button instead of submit and now it works. Little different, but thanks! Without this blog, I wouldn’t have looked at this at all!

    Comment by Rene — #
  30. Good post! We will be linking to this particularly great post on our site.
    Keep up the good writing.

    Comment by Kendra #

Sorry, the comment form is closed at this time.