As soon as the user clicks the delete button my jQuery script asks the server to delete the selected item.
Now I want my php script to send a success or an error response.
Is it possible to fire the error callback in case the item could not be deleted?
Thanks
my jQuery code:
$.ajax({
type: "post",
url: "myAjax.php" ,
dataType: "text",
data: {
some:data
},
error: function(request,error)
{
// tell the user why he couldn't delete the item
// timeout , item not found ...
},
success: function ( response )
{
// tell the user that the item was deleted
// hide the item
}
);
For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-case which can detect a HTTP response line and replace that with a custom one.
To return a status code in PHP, the simplest way is to use the http_response_code() function, along with the relevant HTTP status code parameter into your website, followed by the exit() command which stops any more output being set.
The http_response_code() function sets or returns the HTTP response status code.
Methods to Set HTTP Status Code Sr.No. This method sets an arbitrary status code. The setStatus method takes an int (the status code) as an argument. If your response includes a special status code and a document, be sure to call setStatus before actually returning any of the content with the PrintWriter.
header("HTTP/1.0 404 Not Found");
header('HTTP', true, 500); // 500 internal server error
// etc etc..
Check out header() for more options. All the HTTP error codes are available for use.
Related question: How do you trigger the "error" callback in a jQuery AJAX call using ASP.NET MVC?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With