-1

My ajax request is hitting an api with rate limits.

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "jsonp",
    dataObj : index,
    success: function (response, textStatus, xhr) {
        console.log('success');
    }
    ,error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    }
});

I would like to know when I hit the rate limit.

But for the requests showing this in the console :

Failed to load resource: the server responded with a status of 429 (OK)

I don't see 'success' or 'error'. It's like success and error are not executed.

Is there a way to popup and alert e.g. ?

I tried complete but it does not work either.

Thank you

stallingOne
  • 3,633
  • 3
  • 41
  • 63

2 Answers2

2

I could not find documentation for dataObj but removing it seemed to make the query run properly.

If you get rid of it the error callback gets executed and you will be able to see error in the console.

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "jsonp",
    success: function (response, textStatus, xhr) {
        console.log('success');
    }
    ,error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    }
});

edit: turns out what you actually want to change is the datatype. Unless you are explicitly also passing a callback you should tell jQuery that you are getting json and not jsonp back.

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "json",
    dataObj: index,
    success: function (response, textStatus, xhr) {
        console.log('success');
    }
    ,error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    }
});
k-nut
  • 3,447
  • 2
  • 18
  • 28
  • I'm afraid `dataObj` is essential. In case of success I'm using it like this : `window.data[this.dataObj].xxx = response.xxx;` cf. answer in previous questions here : http://stackoverflow.com/questions/28646946/fetch-movie-data-from-apis-in-javascript – stallingOne Jul 21 '15 at 14:04
  • Can you point me to the documentation of it? As far as I can tell dataobj is just not a valid property for the ajax function. – k-nut Jul 21 '15 at 14:07
  • does it work when you set the `dataType` to `json` instead of `jsonp` as mentioned in my edit? – k-nut Jul 21 '15 at 19:39
  • also where does the `index` come from that you are using as the `dataObj` parameter? Can you show the surrounding code? – k-nut Jul 21 '15 at 20:10
  • jsonp to json did the trick, `error` and `statusCode` are working. Thanks ! – stallingOne Jul 24 '15 at 07:20
0

Try using the jQuery statusCode object in the settings, like this:

$.ajax({
    url:"https://api.themoviedb.org/xxx",
    crossDomain: true,
    dataType: "jsonp",
    dataObj : index,
    success: function (response, textStatus, xhr) {
        console.log('success');
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('error');
    },
    statusCode: {
      429: function() {
        alert( "Exceeded request limit." );
      }
    }
});