“Ajax Status Zero” Resolution
“Ajax status is zero” is one of the hardest errors to debug especially when the Ajax
readyState is 4. If we looked at the
XMLHTTPRequest documentation, we will find that the
readyState attribute can have one of the following values:
- 0 “The object has been constructed.”
- 1 “The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.”
- 2 “All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.”
- 3 “The response entity body is being received.”
- 4 “The data transfer has been completed.”
This means that although the data transfer is completed, the status is not OK (not 200).
After some hours of investigation, I found that the
XHR (XMLHTTPRequest) zero status means that there is a permission problem of the Ajax request. In other words, the Ajax request is trying to access a domain that the request is not authorized to access.
The other interesting point to mention is that I only faced this issue on Firefox 10 while in Internet Explorer 8, the Ajax request worked without any problems.
To solve this problem, I authorized the Ajax request that comes from the JsTestDriver server to access the application server by setting the
"Access-Control-Allow-Origin" header from my servlet as follows.
"http://127.0.0.1:42442" is the JsTestDriver server address from which the Ajax request is constructed and sent to the application server.
After making this workaround the scenario worked fine on all the browsers, I wish that this tip can be helpful for you.