jQuery Ajax 不会继续执行任何回调 .done、.fail、.always - 为什么?
jQuery Ajax does NOT continue onto any of the callbacks .done, .fail, .always - Why?
我故意放置一个不存在的假 URL 的基本示例。
$.ajax({
url : 'https://google.com/404',
dataType: 'html',
cache : false,
done : function(data, textStatus, jqXHR) {
console.log('data:',data);
console.log('textStatus:', textStatus);
console.log('jqXHR:', jqXHR);
},
fail: function(jqXHR, textStatus, errorThrown) {
console.error('jqXhr:',jqXHR);
console.error('textStatus:', textStatus);
console.error('errorThrown:', errorThrown);
},
always: function( data_jqXHR, textStatus, jqXHR_errorThrown ) {
console.warn('data_jqXHR:',data_jqXHR);
console.warn('textStatus:', textStatus);
console.warn('jqXHR_errorThrown:', jqXHR_errorThrown);
}
}); //end-ajax
我原以为它会采用 .fail 路径,然后是 .always 路径,但两者都没有,除了显示错误的控制台:
GET 'https://google.com/404' 404
如何让 jQuery 或 javascript 抛出 404?例如警报('404 错误')
您需要使用the right syntax
$.ajax({
url: 'https://google.com/404',
dataType: 'html',
cache: false,
})
.done(function() {
alert("done");
})
.fail(function() {
alert("failed");
})
.always(function() {
alert("always");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
我故意放置一个不存在的假 URL 的基本示例。
$.ajax({
url : 'https://google.com/404',
dataType: 'html',
cache : false,
done : function(data, textStatus, jqXHR) {
console.log('data:',data);
console.log('textStatus:', textStatus);
console.log('jqXHR:', jqXHR);
},
fail: function(jqXHR, textStatus, errorThrown) {
console.error('jqXhr:',jqXHR);
console.error('textStatus:', textStatus);
console.error('errorThrown:', errorThrown);
},
always: function( data_jqXHR, textStatus, jqXHR_errorThrown ) {
console.warn('data_jqXHR:',data_jqXHR);
console.warn('textStatus:', textStatus);
console.warn('jqXHR_errorThrown:', jqXHR_errorThrown);
}
}); //end-ajax
我原以为它会采用 .fail 路径,然后是 .always 路径,但两者都没有,除了显示错误的控制台:
GET 'https://google.com/404' 404
如何让 jQuery 或 javascript 抛出 404?例如警报('404 错误')
您需要使用the right syntax
$.ajax({
url: 'https://google.com/404',
dataType: 'html',
cache: false,
})
.done(function() {
alert("done");
})
.fail(function() {
alert("failed");
})
.always(function() {
alert("always");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>