Search code examples
javascriptjqueryajaxthemoviedb-api

do something at unsuccessful jquery ajax request


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


Solution

  • 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');
        }
    });