I am loading an external JSON
file with $.ajax()
, and I am puzzled as to why my error function will only execute a console.log
, or alert, but nothing else? In my example, if the JSON
is found, the screen turns blue. If the JSON
is not found (i.e. I change the name of the test.js
file to something that doesn't exist), the screen should turn red, but this isn't happening. What's wrong with my code?
$.ajax({
url: 'test.js',
dataType: 'json',
timeout: 3000,
success: function( data ) { success() },
error: function( data ) { fail() }
});
function success(){
$("body").css("background", "blue"); //this works!
console.log("success"); //this works!
}
function fail(){
$("body").css("background", "red"); //this doesn't work :(
console.log("fail"); //this works!
}
Thanks,