问题
The jQuery getScript fail function is never called. See this fiddle: http://jsfiddle.net/getsetbro/8xNMs/
$.getScript("http://api.jquery.com/scripts/jquery.NO-SUCH-FILE.js").done(function() {
console.log('yep');
}).fail(function() {
console.log('fail function does not fire fine');
});
And the complete function is never called: http://jsfiddle.net/getsetbro/ns6yQ/
$.ajax({
url: url,
type: 'get',
crossDomain: true,
dataType: 'script',
async:false,
cache:false,
success: function(result) {
console.log('SUCCESS');
},
error: function(result) {
console.log('ERROR');
},
complete: function(result) {
console.log('COMPLETE');
}
})
Oh, and in IE it actually fires SUCCESS and COMPLETE when it should have failed. =[
回答1:
.fail
is not working for cross-domain request.
// Bind script tag hack transport
jQuery.ajaxTransport( "script", function(s) {
// This transport only deals with cross domain requests
if ( s.crossDomain ) {
...
script = document.createElement( "script" );
Script element fires no errors and such.
But it's ok for same domain. http://jsfiddle.net/8xNMs/2/
回答2:
cross-domain .fail & .always work with jQuery 2.0
$.getScript("http://api.jquery.com/scripts/NO-SUCH-FILE.js")
.done(function() {
console.log("done fired");
}).fail(function() {
console.log("fail fired");
}).always(function() {
console.log("always fired");
});
http://jsfiddle.net/c2gyy/1/
来源:https://stackoverflow.com/questions/13476497/jquery-getscript-function-never-fails