tags:

views:

39

answers:

5

Hi guys. I'm using jquery and php for some async job. I'm using $.ajax with beforesend, complete, success and error. I made the async POST request to a php that execute some mysql query and return true (string "1" in fact) if all is ok and false (string "0"). Back to js I procedd the data return in the success function (or complete?). Should I have to handle data with

if (data=="1")
  alert("ok")
else
  alert("ko")?

Is this the correct way?

Another question, the error function of $.ajax is for the ajax request itself nad not for the result of the operation? Thanks

A: 

This works! If you want to call your error function you need to setup youre server to respond with an error code, i.e.:

<?php header('HTTP/1.1 404 Not Found'); ?>
jigfox
That's terrible; 500 internal server error suggest that the script crashed, which it did not.
Johannes Gorset
you're right bad example. I've change it
jigfox
A: 

Read this: Model-View-Controller in jQuery

best way to do it...

Reigel
A: 

The function passed as the error parameter of the $.ajax() function will only be run if the request fails; ie. if it returns an error code like 404 Not Found or 500 Internal Server Error.

As far as JavaScript is concerned, the request succeeds regardless of whether it contains "false", "error" or any other string. You'll need to verify this in the function you passed as the success parameter, like you demonstrated above.

You might want to consider returning an HTTP error code (like 403 Forbidden or any other status code that makes more sense to your script) instead of 0 if the PHP script returns false.

Johannes Gorset
A: 

maybe I didn't explain my self very well. The ph script do only a few query but there is a possibility that the query will not run for any reason, so i test the execution of the query and return immediately "0" or at end after all query executed correctly, "1". In javascirpt if I have "0" I have to display an error with alert, if "1" a success message.

So in js I need only to test the returned data and display the alert in the success function of $.ajax?

Kreker
Don't respond to your own question with an answer. Use the comments to respond to individual answers or edit your original question.
JasCav
+2  A: 

For this kind of situation I like to use an object I return as JSON so it's a bit more flexible:

$resultObj = new stdClass ();
$resultObj->success = true;
$resultObj->msg = '';

//your code here
//in case of SQL error
$resultObj->success = false;
$resultObj->msg = 'There was an error while processing your requests';

//return the response
$echo json_encode($resultObj);

And then in your jQuery request you can set the dataType property to json so that it will turn the response into an object in case of HTTP success so you can handle the error in the success function:

$.ajax({
 url      : 'yourpage.php',
 success  : function(data, status, xhr) {
   if (data.success === true) {
      alert('Everything ok');
   } else {
      alert('An error occurred: ' + data.msg);
   }
 },
 error  : function(xhr, status, ex) {
   alert('An error occurred during the communication with the server: ' + xhr.status +' - ' + xhr.statusText);
 },
 dataType : 'json'
});

As others have said you can have your PHP script return a HTTP 404 error so that instead your error is redirected to the javascript error handler, but I like to keep that error handler for unhandled errors like an untrapped exception or an HTTP failure, and for the errors that I handled on the server side I also handle them on the client-side inside the success function.

Hope this helps.

SBUJOLD