How to send a server error response using php?

2019-04-04 23:39发布

问题:

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
 }
);

回答1:

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?