I have been looking around the web (obviously in wrong places) to find what is the success and failure criteria for ajax(jquery post) purposes.
For example, let’s say I am using ajax to post to a php script. The script can return:
- exit 0
- exit 1
- jason array
When would those return values be translated into success and when into failure?
As you may already know $.ajax() supports beforeSend, success, error and complete callbacks.
So what should my script send in case of success and in case of failure for the appropriate callback to be triggered.
Ben is right, but I’ll expand a little.
jQuery determines success or failure based on the HTTP response code of the page being called. Note that this is not the same as the content in the response that is sent back.
As an example, let’s say that you have your PHP script located at http://url.com/script.php
When a user hits that site, they are going to get a response from the server. That response could be a JSON object, it could be an HTML page, or it could be an empty response. In all of these cases, the response code would likely be 200. A 200 response code means that the server understood the request, accepted it, and sent something back.
Now let’s say that a user tries to hit http://url.com/notascript.php
The server doesn’t know what to do with that (assuming that
notascript.php doesn’t actually exist). It fails the request, and sends back a response — probably with the response code 404 (or something else in the 4xx range).
So if you know the actual URL, then how does jQuery’s error handler ever get called?
Well, let’s say that your AJAX call is trying to load a blog post, and tries to make a call like this:
http://url.com/post?id=5. If your PHP script determines that there is no blog entry with an ID of 5, then you probably shouldn’t send back a successful response. Rather, a 4xx response would be more appropriate. And PHP has a built-in function for that:
<?php http_response_code(404); ?>
Now, the response will be read by jQuery as a failure, and the error handler will be called.
RFC2616, which defines HTTP response codes
I believe it’s based on header codes (ie, 200 – success). You can send your own special errors back though e.g. How to receive error in jQuery AJAX response?
You shouldn’t make Ajax error if you have a problem with the code in the page being requested. As far as Ajax is concerned, so long as the page physically is successfully requested, it has performed properly. Any errors within the page being requested are not a fault of Ajax, so should be handled by your code after a successful load of that page. Using custom headers as in the link above will make it easier for you to do that.
Ajax requests page –> Page loads & no errors –> page request successful –> You Perform result error checking
Ajax requests page –> Page loads & has errors –> page request successful –> You Perform result error checking
As you can see, it doesn’t matter if the page being requested has errors, it’s not an Ajax fault, so shouldn’t be treated as one.
You can select one pattern that always printed for success, other cases like ajax failure can be known via AJAX itself (HTTP response code): for example:
You always print: yes for success and if correct result found You always print: no for success and if incorrect result found
If none of above returned then its ajax failure or if the script prints another word then you need to check your PHP.