0

I am making an ajax call using jQuery. I am aware of the success\failure function of the ajax but how can i determine what happened?

Meaning that if i call an example.php file, if the file exists it will return sucess, right? Now how can i tell if the file did what it was suppoused to do?

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142

3 Answers3

1

you should be returning an apt response from your server page which handles the ajax request. I will love to have one responsecode/status which says whats the status of the operation.

$.post("someurl.php",function(data){
  if(data=="success")
   {
      alert("Successfully Updated");
   }   
   else
   {
     alert("Some error in processing!");
   }
}

and from your php you return "success" if your operation is successful and return "error" if it is not successful.

EDIT : Here is how you do it with jquery ajax jQuery post is a shortform of jQuery ajax method with post Type

$.ajax({
  url: "youruel.php?querystring1=12&id=34", 
  success: function(data){
    if(data=="success")
       {
          alert("Successfully Updated");
       }   
       else
       {
         alert("Some error in processing!");
       }
  }
});

You may add the error event also to handle those too.

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

I would try setting the error handling in your php file and returning the error messages described here:

jQuery Ajax error handling, show custom exception messages

Then you can just look at the xhr messages to see what the error is in jQuery.

Community
  • 1
  • 1
ktaylor
  • 116
  • 1
  • 5
0

I can tell you theoretically how I do it with c# handlers (which you can easily translate)

first the jquery success callback:

success: function (jsonResult, textSuccess) {
        if (jsonResult.Errored) {
          // ouch failed
          $("#helpErrorDiv").show();
        }
        else {
          // do something on success
        }
      }

and the server code, always returning json:

context.Response.ContentType = "application/json";

try    {
  // do something and return successfully a json object
}
catch (Exception ex)
{
  context.Response.Write((new {Errored = true}).ToJson());
}
jenson-button-event
  • 18,101
  • 11
  • 89
  • 155