简体   繁体   English

处理HTTP状态Ajax响应

[英]Handling HTTP status Ajax response

I am attempting to integrate the Instapaper Simple API into something but I am struggling to understand how to handle the response that the API sends back in Javascript. 我试图将Instapaper Simple API集成到某些东西中,但我很难理解如何处理API在Javascript中发回的响应。 The article is adding to Instapaper just fine so I know that the submission is working just not my response handlers. 这篇文章正在添加到Instapaper中,所以我知道提交工作正好不是我的响应处理程序。

This is the code I have so far and I'm guessing that the success function is not the correct way of handling the response. 这是我到目前为止的代码,我猜测成功函数不是处理响应的正确方法。

$.ajax({
    type: 'GET',
    url: url,
    dataType: 'jsonp',
    success: function( data, status ) {
        alert("yay");
    },

    error: function(status) {
        alert("oh noes");
    }
});
return false;

Instapaper returns a 201 when the article has been added. 添加文章后,Instapaper会返回201。 I can see that in the Google Chrome Network tool that the GET returned a 201 status. 我可以看到,在谷歌Chrome网络工具中,GET返回了201状态。 Just wondering how I handle that status within the code above. 只是想知道我如何在上面的代码中处理该状态。

Thanks. 谢谢。

Edit When I click the link to activate the code below it pops up the alter under the error function right now even though it has worked. 编辑当我单击链接以激活下面的代码时,它会立即弹出错误功能下的alter,即使它已经有效。

$.ajax({
  statusCode: {
    201: function() {
      alert("201!");
    }
  }
});

this should work with any http status code 这应该适用于任何http状态代码

jQuery.ajax() provides statusCode map for such purposes: jQuery.ajax()为此目的提供statusCode映射:

$.ajax({
    type: 'GET',
    url: url,
    dataType: 'jsonp',
    statusCode: {
      200: function( data ) {
          alert("yay");
      },
      201: function( data ) {

      }
    },
    error: function(status) {
        alert("oh noes");
    }
});

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM