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
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?
For this kind of situation I like to use an object I return as JSON so it’s a bit more flexible:
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:
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.