Hi, let say we have an ajax that post some data to the server-side, for example some data to be inserted in a table in the database, I think there are two kind of exceptions that could occur : 1) problem inserting data in the database. 2)ajax communication with server side problem. so each one of the two exceptions above will send a json message back to the ajax so the user will know that : 1) submit was successful. 2)submit was not successful (problem inserting in the database) 3) submit was not successful because of ajax communication problem. For me this is the theory, can you please give me an example on how to implement the ajax to deal with the 3 above json messages ? Thanks, your help is appreciated.
The easiest way, given that you're passing the data to some sort of server-side script (for instance a .php-file parsing the data and inputing it to a database), is to return a result from the server-side script - if the data is inserted successfully (or updated, modified in any way according to what you want to do), return a json-encoded success-message, with an optional status as well, and if there is an error with the data being inserted, you can return several different error-messages - you can return error-messages returned by whatever database-engine you're using, or you can set up tests and checks and return based on what fails. As for errors with the ajax-function itself, for instance if it doesn't find the file it's posting to, you can just do a check for the return-message the ajax-function get - you want a 200-return, if you get a 404, 403, 500 etc. it's an error - I suggest reading up on HTTP status codes
Thank you PoPSiCLe, you said : "you can return several different error-messages - you can return error-messages returned by whatever database-engine you're using " my question is how to deal with different error-messages returned by database-engine ? can you give me an example, let say the server side returned 3 DB exception, so 3 messages (jsonMessage1, jsonMessage2, jsonMessage3), the question how to deal with each message in ajax.success method ?? Thanks.
You shouldn't get more than one reply pr instance - say you try to input data that are wrongly formatted, so that the DB doesn't insert a row - it usually won't tell you exactly what's wrong, but it will return a non-inserted (updated rows == 0) which you then can convert to an error message. In my own apps I have something like this: $stmt = $core->dbh->prepare("UPDATE database SET something = :variable_1"); // this assumes you have some sort of DB-class (mine is named Core) if ($stmt->execute(array(':variable_1'=>$variable_1))) { // do stuff here if ($stmt->rowCount() == 1) { $statusmessage = 'success'; $info = 'Record updated'; } else { $statusmessage = 'error'; $info = 'Record not updated'; } else { $statusmessage = 'error'; $info = displayError($stmt); //this is a function within the Core-database-class returning SQL-errors in human readable format } PHP: And the displayError()-function looks like this: function displayErrors($result) { $sqlerror = ($result->errorCode() > 0) ? $result->errorInfo() : ''; if (!empty($sqlerror)) { return '<p class="messagebox error">'.$sqlerror[2].'</p>'; } else { return ''; } } // end displayErrors PHP:
Also you checked with timeout in ajax, if server take time more the timeout mentioned in ajax call it will abort the call. You can get the message from complete function.