Yatin
Yatin

Reputation: 3120

Getting status as 0

Hi I am using the below code for ajax call

alert("4");
  req.onreadystatechange=function()
  {
  alert("5");
  if (req.readyState==4 && req.status==200)
    {
    xmlDoc=req.responseXML;
    alert("xml doc received"+xmlDoc);
    txt="";
    x=xmlDoc.getElementsByTagName("FIRSTNAME");
    y=xmlDoc.getElementsByTagName("LASTNAME");
    alert("Response achieved"+x);
    }
    else
    {
    alert("Error!!! --> req.readyState  "+req.readyState+" Error !!! ---> req.status  "+req.status);
    }
  }


req.open("POST",url,true);
alert("6");
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send(parameters);

I am getting as req.readyState as 4 and req.status as 0 what could be the problem

I am hosting a file on local apache server . Please reply .

Upvotes: 1

Views: 61

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075537

The usual reason for this is that you're trying to make a cross-domain call and being stopped by the Same Origin Policy, which prohibits cross-origin ajax calls. You should be getting an error in your developer console or network tab telling you the request has been prevented.

If you control the server in question or you can contact those who do, you may be able to (get them to) enable access from your origin via Cross-Origin Resource Sharing. Provided they whitelist your origin, that works with all modern browsers, although sadly in IE8 and IE9 you have to use Microsoft's special XDomainRequest object rather than XMLHttpRequest (as of IE10, Microsoft finally joined the party).

Alternately, you might see if the server you're trying to work with supports JSONP (don't confuse this with JSON), although as your request is a POST and JSONP is inherently a GET operation, they may not.

Upvotes: 3

Related Questions