Ajax / php – 如何返回失败

前端之家收集整理的这篇文章主要介绍了Ajax / php – 如何返回失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
jquery允许成功失败和错误响应.如何手动返回故障或错误以及描述故障或错误的字符串?

解决方法

使用简单的 JSON信封报告服务器端发生的故障(不是HTTP错误)已经成为一种惯例.

flickr api提供了一个典型的包装示例:

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

因此,在对flickr API进行Ajax调用的情况下,在成功回调中,您将检查返回的数据是否成功/失败

  1. $.ajax({
  2. //...
  3. success: function(data) {
  4. if(data.stat == 'fail') {
  5. //data.code + data.message contain details that could be used here
  6. }
  7. else {
  8. //do success stuff here
  9. }
  10. }
  11. });

猜你在找的Ajax相关文章