Reputation: 53896
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) {
}
});
Upvotes: 18
Views: 14526
Reputation: 11886
If your URL is a PHP page, you can do something like this:
<?php
header("HTTP/1.0 404 Not Found");
exit();
//the rest of your code
You can simply comment it out later when you're done testing your error function.
Upvotes: 1
Reputation: 30771
You could simply put an incorrect URL into the URL attribute of the AJAX call.
Upvotes: 21
Reputation: 6417
You have to stub the ajax request, and return a custom response that will trigger the error callback. You can do this easily with Javascript testing frameworks such as Jasmine.
Upvotes: 1
Reputation: 30771
What I do is just turn my server off before I make the AJAX request, which will simulate the server not responding.
Remember it'll need to be switched back on before you can refresh for changes.
Upvotes: 3