0

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. The article is adding to Instapaper just fine so I know that the submission is working just not my response handlers.

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. I can see that in the Google Chrome Network tool that the GET returned a 201 status. 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.

Aaron Fisher
  • 645
  • 3
  • 8
  • 23

2 Answers2

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

this should work with any http status code

Jarry
  • 1,891
  • 3
  • 15
  • 27
1

jQuery.ajax() provides statusCode map for such purposes:

$.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/

ioseb
  • 16,625
  • 3
  • 33
  • 29
  • OK, I have added the statusCode part and it is working somewhat. The alert only pops up for the 200 one, if I am not wrong the 200 means OK? but the 201 does not pop up? – Aaron Fisher Nov 27 '12 at 19:13
  • did you put alert() code in 201 callback? i left it empty and it can't do anything if you used my code directly... – ioseb Nov 27 '12 at 19:15
  • Yes, it actually appears that Instapaper returns a 200 for authentication and then returns a 201 to say that the URL has been added. – Aaron Fisher Nov 27 '12 at 19:16
  • Do you know if the status is 'canceled' if there a number for that? If you enter the incorrect username or password it throws a canceled. – Aaron Fisher Nov 27 '12 at 20:15