php - How to receive error in jQuery AJAX response? -
i using jquery $.ajax() function perform database insert operation on php page. so, on success regular operation want ajax call receive error if update operation fails.
so should be:
- $.ajax -> call php function
- receive success on successful record insert
- receive error on unsuccessfull insert
so, should return false; php function? think $.ajax() success or error depends if ajax call successful or not. so, think if return false; php function still considered "success" ajax call. right
you return error message in server response (or using json) suggested. don't because pollutes data returned.
i way i've been using recently, involves creating custom http header inform if there error.
php code:
if($there_was_error){ header("db_success: 0"); //sends custom header }else{ header("db_success: 1"); } jquery code:
$.ajax({ ... success: function(data, status, xhr){ //you can use 'data' var if(xhr.getresponseheader("db_success") == 1){ alert("save successful"); //your regular sutff }else{ alert("save failed"); } } ... }) as can see, created custom http header, called db_success used metadata, can assign server-side (php) , read client-side (javascript) know if there error.
hope helps. cheers
Comments
Post a Comment