Why is AJAX returning HTTP status code 0?
Asked Answered
D

22

153

For some reason, while using AJAX (with my developed application) the browser just stops uploading and returns status codes of 0. Why does this happen?

Dunfermline answered 4/1, 2010 at 16:35 Comment(0)
A
124

Another case:

It could be possible to get a status code of 0 if you have sent an AJAX call and a refresh of the browser was triggered before getting the AJAX response. The AJAX call will be cancelled and you will get this status.

Apian answered 27/9, 2012 at 12:56 Comment(5)
Thanks for pointing this out, it led me to realise that (as well as a browser refresh) a form submission can cancel the ajax call.Adsorb
Just do <form onsubmit="return false;">Putnam
@Heitor's suggestion worked for me -- my form's "submit" button was refreshing the page, meanwhile I had javascript trying to run an ajax call when the "submit" button was clicked. Result was that the ajax call was canceledFestus
Hey, I missed a e.preventDefault();Kassi
OMG... I've spent 3 hours on this. It turns out that the Ajax call ran a script that restarted Apache. It returned nothing, I got a code of 0... D'oh.Narva
C
113

In my experience, you'll see a status of 0 when:

  • doing cross-site scripting (where access is denied)
  • requesting a URL that is unreachable (typo, DNS issues, etc)
  • the request is otherwise intercepted (check your ad blocker)
  • as above, if the request is interrupted (browser navigates away from the page)
Cadence answered 6/1, 2010 at 20:43 Comment(7)
I'm getting it when the connection is refused. For example, when debugging my site with Visual Studio, if I have stopped the debugging session, any request that tried to connect will get net::ERR_CONNECTION_REFUSED and the status code will be zero. That makes sense, since you can't have a status code if you can't connect to the server that's supposed to be providing one.Miry
I can confirm. I've got status==0 after shutting my web server down.Selenodont
i have errors because of 'cross-site scripting'; what should i do?Destroyer
@nasimjahednia lookup CORS... the server you're trying to access needs to allow for cross site scripting from your domain.Cadence
I'm confused. Is there no way to get the HTTP error code returned? This is a lot of different cases here (415, 404, etc?)Adkisson
Is there a difference between cross site scripting for IOS mobile and Android/Windows on the other hand? Because I get status 0 only for mobile IOS...Decrypt
@EdwinEvans there is no HTTP response code since there is never a response from the server in these cases.Upswing
T
9

Status code 0 means the requested url is not reachable. By changing http://something/something to https://something/something worked for me. IE throwns an error saying "permission denied" when the status code is 0, other browsers dont.

Thirtyone answered 23/6, 2011 at 17:3 Comment(1)
In this case, chrome currently will leave the request in a "Stalled" state until it times out. It can be caused by either a Strict-Transport-Security flag returned in a previous request from that server, or it can be a Secure/HttpOnly flag on a cookie being sent with the request.Hoicks
S
9

Same problem here when using <button onclick="">submit</button>. Then solved by using <input type="button" onclick="">

Summand answered 12/3, 2014 at 21:16 Comment(2)
Bingo!! Anyone can explain why this?Paradox
By default HTML button type is 'submit', so clicking it will attempt submit form data. So it will execute the click action, then attempt to submit. If you use type 'button' it is no longer default type and will only perform onclick.Exhibitioner
B
7

It is important to note, that ajax calls can fail even within a session which is defined by a cookie with a certain domain prefixed with www. When you then call your php script e.g. without the www. prefix in the url, the call will fail and viceversa, too.

Biology answered 3/9, 2012 at 11:54 Comment(0)
S
5

Because this shows up when you google ajax status 0 I wanted to leave some tip that just took me hours of wasted time... I was using ajax to call a PHP service which happened to be Phil's REST_Controller for Codeigniter (not sure if this has anything to do with it or not) and kept getting status 0, readystate 0 and it was driving me nuts. I was debugging it and noticed when I would echo and return instead of exit the message I'd get a success. Finally I turned debugging off and tried and it worked. Seems the xDebug debugger with PHP was somehow modifying the response. If your using a PHP debugger try turning it off to see if that helps.

Swartz answered 8/2, 2012 at 18:34 Comment(0)
L
4

This article helped me. I was submitting form via AJAX and forgotten to use return false (after my ajax request) which led to classic form submission but strangely it was not completed.

Longfellow answered 12/3, 2012 at 21:35 Comment(1)
Dawn I hate the fact I was caught by this one again! <form onsubmit="return false;"> did the trick.Putnam
G
4

I found another case where jquery gives you status code 0 -- if for some reason XMLHttpRequest is not defined, you'll get this error.

Obviously this won't normally happen on the web, but a bug in a nightly firefox build caused this to crop up in an add-on I was writing. :)

Glycosuria answered 7/4, 2012 at 21:51 Comment(1)
You made a point, I had a problem with jQuery.ajax() XHR object. The request wasn't even created on AJAX call, still getting f.open is not a function and status code 0. Caused by: I was returning $.ajaxSettings.xhr object from $.ajaxSetup({xhr}), returning new window.XMLHttpRequest(); instead solved the problemSupinator
H
3

"Accidental" form submission was exactly the problem I was having. I just removed the FORM tags altogether and that seems to fix the problem. Thank you, everybody!

Hornback answered 8/8, 2012 at 19:54 Comment(0)
F
3

I had the same problem, and it was related to XSS (cross site scripting) block by the browser. I managed to make it work using a server.

Take a look at: http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/282972/why-am-i-getting-xmlhttprequest.status0

Flugelhorn answered 21/3, 2014 at 14:1 Comment(0)
C
2

In my case, setting url: '' in ajax settings would result in a status code 0 in ie8.. It seems ie just doesn't tolerate such a setting.

Cantilever answered 11/3, 2012 at 7:18 Comment(0)
R
2

We had similar problem - status code 0 on jquery ajax call - and it took us whole day to diagnose it. Since no one had mentioned this reason yet, I thought I'll share.

In our case the problem was HTTP server crash. Some bug in PHP was blowing Apache, so on client end it looked like this:

mirek@toccata:~$ telnet our.server.com 80
Trying 180.153.xxx.xxx...
Connected to our.server.com.
Escape character is '^]'.
GET /test.php HTTP/1.0
Host: our.server.com

Connection closed by foreign host.
mirek@toccata:~$ 

where test.php contained the crashing code. No data returned from the server (not even headers) => ajax call was aborted with status 0.

Rozalie answered 17/12, 2013 at 17:7 Comment(0)
R
2

In my case, it was caused by running my django server under http://127.0.0.1:8000/ but sending the ajax call to http://localhost:8000/. Even though you would expect them to map to the same address, they don't so make sure you're not sending your requests to localhost.

Renvoi answered 17/12, 2014 at 22:56 Comment(0)
P
2

In our case, the page link was changed from https to http. Even though the users were logged in, they were prevented from loading with AJAX.

Pledget answered 19/4, 2018 at 14:40 Comment(1)
interesting. I hope its my case. Since my iframe opens with https and on result success I send ajax to the http then I get status 0Proud
H
1

For me, the problem was caused by the hosting company (Godaddy) treating POST operations which had substantial response data (anything more than tens of kilobytes) as some sort of security threat. If more than 6 of these occurred in one minute, the host refused to execute the PHP code that responded to the POST request during the next minute. I'm not entirely sure what the host did instead, but I did see, with tcpdump, a TCP reset packet coming as the response to a POST request from the browser. This caused the http status code returned in a jqXHR object to be 0.

Changing the operations from POST to GET fixed the problem. It's not clear why Godaddy impose this limit, but changing the code was easier than changing the host.

Heffron answered 31/7, 2014 at 8:15 Comment(0)
H
1

I think I know what may cause this error.

In google chrome there is an in-built feature to prevent ddos attacks for google chrome extensions.

When ajax requests continuously return 500+ status errors, it starts to throttle the requests.

Hence it is possible to receive status 0 on following requests.

Herein answered 8/5, 2017 at 2:18 Comment(0)
A
1

In an attempt to win the prize for most dumbest reason for the problem described.

Forgetting to call

xmlhttp.send(); //yes, you need this pivotal line!

Yes, I was still getting status returns of zero from the 'open' call.

Alyworth answered 29/6, 2017 at 0:55 Comment(0)
R
1

In my case, I was getting this but only on Safari Mobile. The problem is that I was using the full URL (http://example.com/whatever.php) instead of the relative one (whatever.php). This doesn't make any sense though, it can't be a XSS issue because my site is hosted at http://example.com. I guess Safari looks at the http part and automatically flags it as an insecure request without inspecting the rest of the URL.

Ram answered 26/12, 2018 at 18:49 Comment(0)
U
1

In my troubleshooting, I found this AJAX xmlhttpRequest.status == 0 could mean the client call had NOT reached the server yet, but failed due to issue on the client side. If the response was from server, then the status must be either those 1xx/2xx/3xx/4xx/5xx HTTP Response code. Henceforth, the troubleshooting shall focus on the CLIENT issue, and could be internet network connection down or one of those described by @Langdon above.

Ursuline answered 5/4, 2019 at 17:7 Comment(0)
I
1

In my case, I was making a Firefox Add-on and forgot to add the permission for the url/domain I was trying to ajax, hope this saves someone a lot of time.

Ind answered 9/2, 2021 at 21:29 Comment(0)
N
0

Observe the browser Console while making the request, if you are seeing "The Same Origin Policy disallows reading the remote resource at http ajax..... reason: cors header ‘access-control-allow-origin’ missing" then you need to add "Access-Control-Allow-Origin" in response header. exa: in java you can set this like response.setHeader("Access-Control-Allow-Origin", "*") where response is HttpServletResponse.

Nepenthe answered 13/9, 2018 at 6:14 Comment(0)
A
0

for me it was a Base URL for the application was "https://www.website.com"

and some users were typing "website.com" - without the "www"

The resulting AJAX was returning an error status code of ZERO '0' right out of the gate as;

it's origin protocol did not match its destination.

This was particularly difficult to debug, as the first instances were coming from mobile devices and we could not trace it properly.

Attaway answered 11/3, 2023 at 1:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.