How to cancel an ajax request (from jquery) on the server side?
Sounds like a weird question, but I'll say that I have something like this:
$.post( "/myajax.php",
{ "param1": value1, "param2": value2 },
function( data, status ) {
if( status == "success" ) {
$("#someid").html( data );
}
}, "html" );
While myajax.php does whatever it needs to do, it decides that it doesn't need to make any changes to #someid
. If it just returns the item is someid
empty, but I want to leave it alone. How can php request return in such a way that status != "success"
?
source to share
ajax calls treat any 200 OK
as success. as such, you can return something else from your PHP script to cause an error in your ajax.
here are some other status codes if you want to choose something more appropriate.
edit : I don't always agree with this approach, but I'm still leaning more towards working on a status message (http status) rather than a response body.
My thoughts are that in a subsequent transaction as stated, you are updating the resource. you can reasonably assume that you expect one of two things to happen (under "success" conditions):
-
200 OK means the content was submitted without issue, and the response received should normally show new content. the ajax method "enforces" this behavior, allowing you to get the response body to update your user interface.
-
in a scenario where no update is required, 200 OK is also fine (since the request handled as expected), but perhaps 204 No Content is better, as it indicates the request has been completed but no view change is required. I tend to believe that this has more to do with the ajax call, as you can ignore the response body (since there isn't one) and work with the status (204? update the interface to say "no changes have been made" or similar)
source to share