Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

success and failure criteria for ajax(jquery post) purposes

Tags:

ajax

php

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
  • return
  • etc...

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.

like image 728
Kam Avatar asked Mar 01 '13 21:03

Kam


People also ask

How does jQuery AJAX determine success?

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. When a user hits that site, they are going to get a response from the server.

How do you write if condition in AJAX success?

ajax({ url: $(this). attr('href'), type: $(this). attr('method'), dataType: 'json', success: function (data) { //PROBLEM HERE ********** if(data. success == 'true' ){ alert('true'); } else { alert('false') }; ;} });

What triggers AJAX success?

Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the . ajaxSuccess() method are executed at this time.


2 Answers

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.

Further reading:

http_response_code() function

RFC2616, which defines HTTP response codes

REST

like image 151
Thomas Kelley Avatar answered Nov 02 '22 23:11

Thomas Kelley


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

vs

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.

like image 22
BenOfTheNorth Avatar answered Nov 03 '22 01:11

BenOfTheNorth