Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP trigger AJAX error code without using array

I want to be able to get a PHP script called via AJAX to return with an error code that the jQuery AJAX handler error: will handle. I don't want to use a JSON array - I'd like to keep it as clean as possible. Can someone point me in the right direction?

Thanks,

James

like image 370
Bojangles Avatar asked Dec 06 '22 22:12

Bojangles


1 Answers

If you want to trigger the AJAX error handler, just pass back something other than a 200! Try this:

<?php
  header("HTTP/1.0 404 Not Found");
  exit();
?>

Just remember to do two things:

  1. Try to send the correct error code to comply with HTTP methods. i.e. if you page throws an error, then you should return a 500, etc. You can see a reference here at the w3.org site.
  2. Make sure you send the header before ANY other content except whitespace.

This should be the clean solution you are going for.

like image 159
Groovetrain Avatar answered Dec 10 '22 13:12

Groovetrain