我正在构建一个随机引用机器(学习AJAX)。 AJAX请求有效,而.done()已成功调用。现在我正在尝试进行错误处理,当AJAX请求实际上不成功时,我的.fail()不起作用。 任何想法在这里可能是错的?谢谢! `
var quoteRequestTimeout = setTimeout(function(){
if ($.isEmptyObject(backupQuote)){
$quoteText.text('Failed to get a new quote');
$author.text('');
}
else {
$quoteFail.text('Failed to get a new quote');
$quoteText.text(backupQuote.quoteText);
$author.text(backupQuote.quoteAuthor);
};
}, 8000);
$.ajax({
url: "https://api.forismatic.com/api/1.0/",
jsonp: "jsonp",
dataType: "jsonp",
data: {
method: "getQuote",
lang: "en",
format: "jsonp"
},
cache: false
})
.done(function(data, status, jqXHR) {
console.log(data);
$quoteText.text(data.quoteText);
$author.text(data.quoteAuthor);
backupQuote = {
quoteText: data.quoteText,
quoteAuthor: data.quoteAuthor
};
$quoteFail.text('');
clearTimeout(quoteRequestTimeout);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown.toString());
console.log("it worked");
});`