lundi 13 juin 2016

How to ignore a 404 error in an AJAX call


I have an AJAX call to retrieve some data in JSON format. And that data I want to use for something. However, if there is no data, I want something else to happen. I have this function which works exactly how I want it to:

var config = {
      type: "GET",
      url: /my/url/,
      success: function(context) { do_something(context); },
      error: function(xhr,ajaxOptions, thrownError) { if (xhr.status == 404){do_something_else()}}
    };
    $.ajax(config);

However, in the console I still get:

404 NOT FOUND

How can I ignore/catch that error, I thought this is being done with the error:... line?

EDIT

I tried this

var config = {
          type: "GET",
          url: /my/url/
        }
        $.ajax(config);

    $( document ).ajaxComplete(function( event, xhr, settings ) {
      if (settings.url === /my/url/){
        if (xhr.status == 404){
          do_something_else();
        }
        else if (xhr.status == 200){
          do_something();
        }    
      }
});

Aucun commentaire:

Enregistrer un commentaire