XMLHttpRequest 0 state (responseText is empty)

Unable to retrieve data with XMLHttpRequest (status 0 and responseText is empty):

 xmlhttp = new XMLHttpRequest ();
 xmlhttp.open ("GET", "http://www.w3schools.com/XML/cd_catalog.xml", true);
 xmlhttp.onreadystatechange = function () 
 {
   if (xmlhttp.readyState == 4)
     alert ("status" + xmlhttp.status);
 }
 xmlhttp.send ();

It alerts "status 0".

The same situation with the localhost request (cd_catalog.xml is saved as a local file)

 xmlhttp.open ("GET", "http: //localhost/cd_catalog.xml", true);

But asking for localhost IP address

 xmlhttp.open ("GET", "http://127.0.0.1/cd_catalog.xml", true);

and requesting a local file

 xmlhttp.open ("GET", "cd_catalog.xml", true);

everything is in order (status 200)

What can cause the problem (status = 0) with an online request?

PS: Live HTTP Headers shows that everything is in order in all four cases:

   HTTP / 1.1 200 OK
   Content-Length: 4742

PS2: local Apache web server on VMWare (Win7 host system, Ubuntu guest OS, network adapter - NAT). Browser - Firefox.

+90
javascript ajax
Feb 15 2018-11-15T00:
source share
20 answers

The status is 0 when your HTML file containing the script is opened in the browser through the file scheme. Be sure to place the files on your server (apache or tomcat), and then open it using the http protocol in your browser. (i.e. http: //localhost/myfile.html ) This is a solution.

+39
Apr 16 2018-12-12T00:
source

The cause of your problems is that you are trying to make a cross-domain call, and it fails .

If you are doing local development, you can make cross-domain calls - I do this all the time.

For Firefox you need to enable it in the configuration settings

signed.applets.codebase_principal_support = true 

Then add something like this to your open XHR code:

  if (isLocalHost()){ if (typeof(netscape) != 'undefined' && typeof(netscape.security) != 'undefined'){ netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead'); } } 

For IE, if I remember correctly, all you have to do is enable the browser security option in the "Miscellaneous and rarr; access to data sources by domain" section to make it work with ActiveX XHR.

IE8 and above also added cross-domain capabilities to native XmlHttpRequest objects, but I haven't played with them yet.

+27
Mar 08 '11 at 1:27
source

Actually, make sure that your button type is the Do Not Send button, which caused a state conflict that I recently met.

+25
Dec 29 '11 at 17:14
source

If the server responds to the OPTIONS method and GET and POST (which one you are using) with a header, for example:

 Access-Control-Allow-Origin: * 

It may work fine. It seems to be in FireFox 3.5 and rekonq 0.4.0. Apparently, with this header and the initial response to OPTIONS, the server tells the browser: "Go ahead and let this cross-domain request go through."

+16
Sep 30 '11 at 9:40
source

Consider also the request timeout :

A modern browser returns readyState = 4 and s tatus = 0 if too much time elapses before the server response.

+9
Jun 29 '16 at 10:56 on
source

Add setRequestHeader("Access-Control-Allow-Origin","*") in response to your server.

+6
May 05 '13 at 10:44
source

I had a similar problem. Everything was in order, "readistate" was 4, but the "status" was 0. This was because I used a portable Apache PHP server, and my file, in which I used the XMLHttpRequest object, was an html file. I changed the file extension to php and the problem was resolved.

+3
Nov 07 '11 at 3:16
source

Open the javascript console . There you will see an error message. In my case, it was CORS.

+3
Dec 29 '17 at 23:35
source

To answer the question about why http://127.0.0.1/cd_catalog.xml does not work during http://localhost/cd_catalog.xml : Firefox treats 127.0.0.1 and localhost as two different domains.

+2
May 30 '13 at 10:09
source

Here is another case in which status === 0 , specific to loading:

If you attach the 'load' event handler to XHR.upload , as suggested by MDN (scroll down to the "Monitoring" progress' download part), the XHR object will have status=0 , and all other properties will be empty. If you attach the 'load' handler directly to the XHR object, just like when loading content, you should be fine (unless you use localhost).

However, if you want to get good data in the 'progress' event handlers, you need to bind the handler to XHR.upload , and not directly to the XHR object itself.

I have tested this so far on Chrome OSX, so I'm not sure how much of the problem the MDN documentation is and how much the Chrome implementation is implemented ...

+1
Apr 18 '15 at 22:46
source

Alex Robinson already (and the first) gives the correct answer to this question. But to clarify this a little more ...

You should add an HTTP response header:

Access-Control-Allow-Origin: *

If you do this, the result will not only “work”, but “will work”.

NB What you need to add is the HTTP response header, so you can only do this on the server you are managing. It is not possible to directly obtain http://w3schools.com/XML/cd_catalog.xml from its original URL using XMLHttpRequest (according to the OP question), since this resource has not (at least not since April 24, 2015) include any such CORS header.

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing provides additional information.

+1
Apr 24 '15 at 9:01
source

To see what the problem is, when you get the cryptic error 0, go to ... | More tools | Developer Tools (Ctrl + Shift + I) in Chrome (on the page with the error message)

Read the red text in the log to get a true error message. If there is too much, right-click and clean the console, then repeat the last request.

The first problem was that I first passed the authorization headers to my cross-domain browser web service.

I already had:

 Access-Control-Allow-Origin: * 

But no:

 Access-Control-Allow-Methods: GET, POST, PUT, DELETE Access-Control-Allow-Headers: Authorization 

in the response header of my web service.

After I added this, zero of my error disappeared from my own web server, and also when the index.html file was run locally without a web server, but it still threw errors in the pen code.

Back to ... | More tools | The developer tools so far receive an error message in codepen, and it is clearly explained there: codepen uses https, so I can not call on http, because security is lower.

So I need to host my web service on https.

Knowing how to get a true error message is priceless!

+1
Sep 07 '18 at 17:11
source

My problem like this was solved by checking my html code. I had an onclick handler in my button to submit the form to the method. for example: onclick="sendFalconRequestWithHeaders()" . This method, in turn, calls ajax just like yours and does what I want. But not as expected, my browser did not return anything.

Extracted from someone difficult , I returned false in this handler and decided. Let me mention that before arriving at this post, I spent a whole 3-day weekend and half a day in the office writing code that implements CORS filters , jetty config , other jersey and embedded jetty related things - just to to fix this. Revolving all my understanding around cross domain ajax requests and standards. It was funny how simple javascript errors make you stupid.

To be true, I tried signed.applets.codebase_principal_support = true and wrote isLocalHost() **if** . maybe this method should be implemented by us, firefox says that there isn’t such. Now I have to clear my code to send the git patch. Thanks to this, someone.

0
Nov 25 '13 at 8:57
source

The browser request “127.0.0.1/somefile.html” does not change to the local web server, and “localhost / somefile.html” may come as “0: 0: 0: 0: 0: 0: 0: 1 / somefile.html "if IPv6 is supported. Thus, the latter can be treated as a transition from a domain to another.

0
Aug 10 '15 at 17:00
source

Alex Robinson and bmju have provided valuable insights into understanding cross-origin issues. I would like to add that you may need to make an explicit OPTIONS call in the client code before doing the desired GET / POST (for example, regarding the CORS OAuth service endpoint). Your browser / library cannot automatically process the OPTIONS request. Gruber, this is one of the possible answers to your question.

0
Sep 02 '15 at 8:08
source

I had the same problem (readyState was 4 and status 0) , then I followed a different approach described in this lesson: https://spring.io/guides/gs/consuming-rest-jquery/

He did not use XMLHttpRequest at all, instead used the jquery $ .ajax () method:

 <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="hello.js"></script> </head> <body> <div> <p class="greeting-id">The ID is </p> <p class="greeting-content">The content is </p> </div> </body> 

and for the public /hello.js file (or you can paste it into the same HTML code directly):

 $(document).ready(function() { $.ajax({ url: "http://rest-service.guides.spring.io/greeting" }).then(function(data) { $('.greeting-id').append(data.id); $('.greeting-content').append(data.content); }); }); 
0
Nov 23 '16 at 11:54 on
source

I had to add my current IP address (again) to the Atlas MongoDB whitelist and therefore got rid of status error 0 XMLHttpRequest

0
Sep 28 '18 at 17:07
source

I just had this problem because I used 0.0.0.0 as a server, changed it to localhost and it works.

0
Mar 29 '19 at 13:45
source

In case you make an xhr request in a for loop, it will not work correctly. Follow this topic to learn how to execute an xhr request in a for loop. It worked for me.

XMLHttpRequest for for loop

0
Jun 27 '19 at 12:49 on
source

Edit: Please read Malvolio's comments below, as this answer is deprecated.

You cannot run cross-domain XMLHttpRequests.

Calling 127.0.0.1 works because your test page is at 127.0.0.1 , and the local test also works, because ... it's a local test.

The other two tests do not work, because JavaScript cannot communicate with the remote server via XMLHttpRequest.

Instead, you might think:

  • XMLHttp request of your own server to get your remote XML content for you (e.g. php script)
  • Trying to use a service like GoogleAppEngine if you want to keep it in full.

Hope that helps

-2
Feb 15 '11 at 16:16
source



All Articles