4

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

4 Answers 4

13

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Remember to exit() or die() after calling header(). The call to header() will NOT stop execution of your script the way that return will.
@Scott-Saunders you're right, as I see in your answer. Code edited and your answer and comment voted up.
If you're including an error message from php inside your exit() (eg exit('That value was invalid!');, can you still use this method to trigger AJAX error handling?
@michael-martin-smucker Yes, it will still print the error message that jQuery can pick up as content. Good thing to remember however, is to return the type of content that jQuery expecting. i.e. if jQuery is expecting json, you may want to return exit('{"error":"That value was invalid!"}')
2

Try this:

header("HTTP/1.0 404 Not Found");
exit();

Comments

1

Ajax transmission does not have to be JSON. It can be xml, json, script, or html. By default AJAX uses XML (the X from AJAX stands for that). I see that you are using jQuery. If you are using ajax method you have a dataType parameter that you can modify.

As you speak of it JSON is one of cleanest in my opinion, but if you mean simple you can use text instead.

1 Comment

Thanks for that and yeah, I agree, JSON is very clean. What I meant however was how I can get the script to return so that the code inside the jQuery AJAX error: function is executed. Something like making the PHP exit with exit(1) or something and getting the JS to see that and throw an error to the function error: function(request, status, errorCode) in the JS. Sorry if I was unclear.
0

EDIT: jQuery'ajax'statusCode' method works, but only in jQuery 1.5

I have tried jquery's ajax's 'statusCode' method, works now, with jquery 1.5 but did not get any results... but it should be the way to go...

if my php script does this:

 //localhost/dev/false.php
 header("HTTP/1.0 404 Not Found");  
 exit();

and my javascript does that:

$.ajax({
  url:'http://localhost/dev/false.php',
  statusCode: {
                  404: function() {
                      alert('page not found');
                  }
              }
          });

5 Comments

Umm... Why are you posting this as an answer? Would it not be wiser to post it as your own question - you get more answers that way :-)
Yes, sorry - it was not really a full answer, I guess I should have posted it as a comment. But if you consider my edit "works with jquery 1.5', then it's a correct answer. Thanks for the advise though. :)
Looking at my first comment, I didn't mean to flame you lol - my bad; I just think it's much better if you ask it as a question - you get much more helpful answers. Still, your edits to make it an answer - I'll look into statusCode when I need it (very handy!). At the moment, I only need a 'true or false' error; nothing fancy ;-)
Cool - statusCode seems handy indeed to tailor your callback more precisely with less code... :)
Thanks for pointing it out to me :-) I've been using JSON up till now and echoing lots of HTML using $return['html'] .= where every echo should be is incredibly boring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.