views:

1109

answers:

6

How do ajax know whether it failed or succeeded if server side doesn't echo anything back?

$.ajax(error:..,success:..)

I met with this exception in my test:

uncaught exception: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.statusText]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: http://localhost/script/tab.js :: anonymous :: line 69" data: no]

The server side code is :

$id = process();

And for the purpose of testing,I have exit() in process();

Is that the reason for this exception?If so,why?

EDIT I looked over to the line that cause exception,it's the error handling function of $.ajax()

error:function(XMLHttpRequest, textStatus, errorThrown){ 
    alert(XMLHttpRequest.statusText);alert(textStatus);alert(errorThrown);
}

Anything wrong here?

A: 

This depends on the status code the request returns. A successful request returns a status code in the range of 2xx, an error is in the range of 4xx of 5xx.

For more information see Wikipedia: List of HTTP status codes.

Edwin V.
How is the status code generated from server side?
Shore
This is a header variable that you need to send with your response. For example in PHP you can do header("HTTP/1.0 404 Not Found");, which is a response with status code 404.
Edwin V.
A: 

It would still get a response from the server, with the data part of it being empty. If it got no response at all, that would be an error.

rikh
A: 

http://docs.jquery.com/Ajax/jQuery.ajax#options

Give an option for success and error These functions will be called after the call is made.

Natrium
A: 

There are four possible scenarios that you could get:

  • the server isn't there or refuses the connection (this is identifiable by the sockets library that the browser uses, which will report the connection failure)
  • the connection works and the server returns a non-success error code - this comes back in the header. Indeed, the request can succeed (200 code) even with an empty body, that's perfectly valid
  • the connection comes up but the server fails to respond - I'm not clear on the details of this, but i'd expect the JS to eventually time out because no response was received and return a failure based on that.
  • the connection comes up but the server responds incorrectly (e.g. no headers) - the JS implementation should return this as an error.

In any case, all the error scenarios are handled by the Javascript core's XMLHttpRequest implementation - jQuery just wraps it up with slightly tidier interface.

In your case (now you've provided more information) I would recommend that you use Firebug to see what the server response to your request is. That said, you shouldn't be getting an exception for anything inappropriate from the server, the JS should call the same error callback for all the above cases.

ijw
Added #4 - invalid return format. Likely you're causing an invalid HTTP response, but that's impossible to say since we ahve no idea what server-side language you're using...
ijw
The server response nothing to my request.
Shore
I'm using PHP.
Shore
"Nothing" - no body? No headers? What?
ijw
The header is normal.But nobody.Strange enough,it doesn't report now.
Shore
+1  A: 

The httprequest also returns a status such as 200 == ok, 404 == not found, 12152 == connection closed by server and so on.. Just read up on the status id's what they mean so you can look for them. you can also for debugging reasons just write out myhttprequest.status to the document and it shows what status it returned.

Jonas B
But I can see nothing about status code from firebug.
Shore
A: 

are you missing { } ?

$.ajax(error:..,success:..)

should be

$.ajax( { error: function( ){ } } );

if that's it, sorry dude, that would be annoying to have spent that much time on, haha

Dan Beam