Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest status 0 (responseText is empty)

Cannot get 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 with the localhost IP request

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

and with the local file request

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

everything is OK (status 200)

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

PS: Live HTTP Headers shows that everything is OK in all 4 cases:

   HTTP/1.1 200 OK   Content-Length: 4742 

PS2: Apache local web server on VMWare (host OS Win7, Guest OS Ubuntu, Network adapter – NAT). Browser – Firefox.

like image 900
arigasa Avatar asked Feb 15 '11 15:02

arigasa


People also ask

How do I check XMLHttpRequest status?

status property returns the numerical HTTP status code of the XMLHttpRequest 's response. Before the request completes, the value of status is 0. Browsers also report a status of 0 in case of XMLHttpRequest errors.

What is responseText?

The responseText method is used for all formats that are not based on XML. It returns an exact representation of the response as a string. Plain text, (X)HTML, and JSON are all formats that use responseText.

What is XHR responseText?

responseText. The read-only XMLHttpRequest property responseText returns the text received from a server following a request being sent.


2 Answers

status is 0 when your html file containing the script is opened in the browser via the file scheme. Make sure to place the files in your server (apache or tomcat whatever) and then open it via http protocol in the browser. (i.e. http://localhost/myfile.html) This is the solution.

like image 168
Abhishek_8 Avatar answered Oct 05 '22 22:10

Abhishek_8


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

If you're doing localhost development you can make cross-domain calls - I do it all the time.

For Firefox, you have to enable it in your config settings

signed.applets.codebase_principal_support = true 

Then add something like this to your XHR open code:

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

For IE, if I remember right, all you have to do is enable the browser's Security setting under "Miscellaneous → Access data sources across domains" to get it to work with ActiveX XHRs.

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

like image 27
Slammer Avatar answered Oct 05 '22 22:10

Slammer