jQuery json处理错误

I'm using jquery's ajax function to send json to a php script and return a json set of values for front end processing.

The php script is sending the json and I am able to echo out the json from the php script and retrieve via the success function parameter.

My question is how should I be handling errors. Is there a preferred way to send exception information (validation, etc.) back to the front end, and indicate that an error has occurred in php. Should I just be packaging up such information in a json object and checking what was received.

Thanks,

$.ajax({
   url: 'gear.php',
   type: 'POST',
   data: {'json': JSON.stringify(shopping)},
   success: function(data){
      console.info(data);
   },
   error: function(data){
      console.info(data);
   }
});

I would recommend both approaches actually. Use the error: block to catch any server side errors (i.e. 404, 500 errors, etc), but for business exceptions respond with a different block of JSON that you can check for. Do that in success:.

For example:

$.ajax({
   url: 'gear.php',
   type: 'POST',
   data: {'json': JSON.stringify(shopping)},
   success: function(data){
      if (data.error_message){
          alert(data.error_message);
      } else {
          console.info(data);
      }
   },
   error: function(data){
      console.info(data); //popup an uh-oh message.
   }
});