2

I'm struggling to understand how to get jQuery.ajax to interpret a failed MySQL UPDATE query as an error. Here's my code:

jQuery:

$.ajax({
    type:'POST',
    url:'/ajax/registration.php',
    data:{formData: formData},
    success:function(data){eformDef.forms.registration.success(data);},
    error:function(data){eformDef.forms.registration.success(data);}
});

PHP

$data = $_POST['formData'];
$firstName = mysql_real_escape_string($data['first name']);
$lastName = mysql_real_escape_string($data['last name']);
$email = mysql_real_escape_string($data['email address']);
$password = mysql_real_escape_string($data['password']);


mysql_query("INSERT INTO users (first_name, last_name, email, password) VALUES ('$firstName', '$lastName', '$email', '$password')") or die(mysql_error());

No matter what--even if I make the mysql_query syntax invalid--the ajax call always interprets the response as a success. I assume that's because the mysql_error() gets sent back as the response, the ajax call reads that as a string, and apparently considers that to be a success. Of course I can deal with this by checking the response string in the $.ajax success function, but this seems a bit illogical. In cases where I can test for a data type using the $.ajax dataType option, this isn't an issue. But with an UPDATE query like the one above, I always get a string as a response.

Since there is a workaround (checking the string in the success function), this isn't a critical issue. But if there is a more correct way of doing this, I'd much prefer that.

2 Answers 2

5

PHP will send a 200 OK status no matter what*, so there is no way to jQuery to detect an error.

You have two possibilities:

  • Have the PHP script explicitly output something like "OK" or an JSON encoded array with a status code. Have the jQuery side explicitly look for that "OK" and throw an error if it's not present.

  • Have the PHP script throw an error status code if an error occurs, for example:

      $query = mysql_query(......);
    
      if (!$query)
       {
         header("HTTP/1.1 500 Internal Server Error");
         echo mysql_error(); // Detailed error message in the response body 
         die();
       }
    

* Except for this exception.

5
  • Thanks, this works great. Regarding that exception however... It seems to only affect a 500 error. Could I just pass a different error number, such as 404, to gain the same functionality without worrying about the exception you linked to?
    – maxedison
    Feb 26, 2011 at 18:19
  • @maxedison if you explicitly throw a 500, that should be fine. You could also throw some other error code, though, there is nothing wrong with that. w3.org/Protocols/rfc2616/rfc2616-sec10.html
    – Pekka
    Feb 26, 2011 at 18:41
  • In that case, I guess I don't understand what relevance the "exception" has that you pointed out. I thought you pointed it out to say that throwing a 500 error would always work, except for this kind of exception. No?
    – maxedison
    Feb 26, 2011 at 19:02
  • @max no, I meant that as an exception to my first sentence "PHP will send a 200 OK status no matter what" (hence the star). You can basically throw whatever status code you want, including a 500 (although it's elegant to throw one that fits the problem, like 404 for an invalid record ID. For a database error, a 500 totally makes sense)
    – Pekka
    Feb 26, 2011 at 19:04
  • Thanks. Should have noticed which part the exception was referring to :)
    – maxedison
    Feb 26, 2011 at 19:48
2

The Ajax call will only interpret the response as an error if the HTTP status code indicates that there was an error. die does not set the HTTP status code to 500 (at least the PHP documentation doesn't say so). If you throw an exception in your code instead of just using die, then PHP should return a 500 HTTP status.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.