开发者

Ajax / php - How to return failure

jquery allows开发者_如何转开发 for success failure and error responses. How can i return a failure or error manually along with a string that describes the failuer or error?


It has become somewhat of a convention to utilize a simple JSON envelope to report failures that occurred on the server side (that are not HTTP errors).

The flickr api provides a typical packaging example:

look at the bottom of this page for success and failure examples

So in the case of an Ajax call to the flickr API, in your success callback you would check the returning data for success/failure

$.ajax({   
  //...   
  success:   function(data) {
    if(data.stat == 'fail') {
       //data.code + data.message contain details that could be used here
    }
    else {
      //do success stuff here
    }
  }
});


The success and error callbacks that jQuery provides are for the XMLHTTPRequest object. The only way you would be able to use it is if your server returns an error code in the header of the HTTP response, which isn't very common, so I wouldn't recommend it. In short, the jQuery error function is for fatal errors, like the URL does not exist, or authentication required.

Any other errors that come up should either be handled on the server-side or the success function of the jQuery ajax call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜