9

all. I am using a jquery version 1.6.2. I do a ajax call like this:

  var jqxhr = $j.post(myPHP, function() {
          alert("success");
        })
        .success(function() { alert("second success"); })
        .error(function(data) { alert(JSON.stringify(data)); })
        .complete(function() { alert("complete"); });

The script, run, and show error, than, complete. Some people may thing that...myPHP have some problem, but the myPHP is always show:

{"sayHi":"hihi"}

So, I go to firebugs to check whether the link have any problem, when I called..: It show me the POST url with status 200, which is ok. Also, I can see the respond via firebugs.... But the question is ....why the jquery so me have a error...: And here is the error msg:

{"readyState":0,"responseText":"","status":0,"statusText":"error"}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Tattat
  • 15,548
  • 33
  • 87
  • 138
  • 3
    Solved, I know wt happen: cross-domain ajax calls.... Related: http://stackoverflow.com/questions/3586780/jquery-ajax-post-unsuccessful – Tattat Jul 25 '11 at 16:15
  • 1
    It is about cross-site scripting: http://stackoverflow.com/questions/3586780/jquery-ajax-post-unsuccessful – Tattat Jul 28 '11 at 09:33

4 Answers4

3

For me this problem was caused by a cross-domain issue. So I had a local html file (c:\test.html) and tried to get data from a server (localhost/servlet). When putting html on the server - the problem was gone.

greeness
  • 15,956
  • 5
  • 50
  • 80
3

If the browser switches the web page while an XHR request is still in progress (the user clicked a link, the back button, …), this XHR request will be cancelled with exactly your error.

Have a look at the following blog post, where the issue is explained in depth: http://bartwullems.blogspot.de/2012/02/ajax-request-returns-status-0.html

Alex Hoppen
  • 338
  • 2
  • 7
1

You forgot to tell jQuery that the server is returning JSON:

   var jqxhr = $j.post(myPHP, function() {
           alert("success");
         }, "json") // here
        .success(function() { alert("second success"); })
        .error(function(data) { alert(JSON.stringify(data)); })
        .complete(function() { alert("complete"); });
karim79
  • 339,989
  • 67
  • 413
  • 406
0

In my case, it was not due to Cross domain. I was not using e.PreventDefault(). See here

Community
  • 1
  • 1
user007
  • 1,504
  • 2
  • 18
  • 51