How to test ajax error callback?

blue-sky picture blue-sky · Sep 17, 2012 · Viewed 11.9k times · Source

Within an ajax request how can the error callback be tested ? Is it possible to simulate a network connection error ?

    $.ajax({
        url: "myUrl",       
        type: 'post',
        dataType : "json",
        data : ({
            myJson
        }),
        success : function(jsonSaveResponse) {  

        },
        error: function (xhr) {

    } 

    }); 

Answer

Mild Fuzz picture Mild Fuzz · Sep 17, 2012

You could simply put an incorrect URL into the URL attribute of the AJAX call.