Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom PHP Errors for AJAX calls

Tags:

jquery

ajax

php

I'm building a web application, and am wondering how to handle errors with my AJAX calls. For instance, if the user enters some sort of data that is invalid (bad email address, user already exists) I want to be able to throw an error from PHP.

I've seen here http://php4every1.com/tutorials/jquery-ajax-tutorial/ that you could just use a JSON object and handle the error report from JQuery's Success function, but that doesn't seem like the right way to do it. It would make sense to me that jQuery's error function should be utilized when there is an error. I guess I'm a stickler for that kind of thing.

Here's how I'm doing it right now.

//In my PHP file called from JQuery
function error($msg) {
    header("HTTP/1.0 555 ".$msg);
    die();
}
//Then that error is handled accordingly from JQuery

So I'm creating an error code of 555—which is not defined as anything—and tacking on my own custom error message. Is that the right way to do this? Should I just use JSON? There's gotta be a standard way to send out error messages like this, right?

If you need to see more of my code to get a better idea, the whole project is up on github: https://github.com/josephwegner/fileDrop. The file in question is config/phpFuncts.php.

like image 563
jwegner Avatar asked Apr 12 '11 15:04

jwegner


People also ask

How do you handle errors in AJAX call?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

Can PHP be used in AJAX?

Start Using AJAX Today In our PHP tutorial, we will demonstrate how AJAX can update parts of a web page, without reloading the whole page. The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.

Why is AJAX success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

What causes AJAX errors?

Many pages send AJAX requests to a server. Because this relies on the cooperation of the server and the network between the client and the server, you can expect these AJAX errors: Your JavaScript program receives an error response instead of data; Your program has to wait too long for the response.


3 Answers

I'd just use a JSON object and an HTTP header of 200. There was nothing wrong with the request itself, and your server behaved as it was supposed to - the error was on a different layer of the abstraction.

like image 84
Jimmy Sawczuk Avatar answered Oct 04 '22 02:10

Jimmy Sawczuk


The 400 family of HTTP status codes indicate that there was a problem with the request. I would set the response code to 400 and include the list of error messages in the response body as JSON.

like image 24
Michael Avatar answered Oct 03 '22 02:10

Michael


I hate using HTTP status codes for indicating failures. The HTTP codes should be reserved for actual HTTP-level errors, and errors related to the AJAX request should be sent back via a JSON structure.

e.g.

$data = array()
if (some big ugly computation fails) {
   $data['errorcode'] = -123;
   $data['error'] = true;
   $data['success'] = false;
   $data['errormessage'] = 'some helpful error message';
} else {
   $data['success'] = true;
   $data['error'] = false;
   $data['response'] = 'whatever you wanted to send back....';
}
echo json_encode($data);

Then on the client-side

if (data.error) {
   alert('Request blew up: ' + data.errormessage);
}
like image 29
Marc B Avatar answered Oct 02 '22 02:10

Marc B