-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to forked jquery.iecors library to fix error handling.
This fixes error handling so that if the CORS request fails, jQuery's error handling is triggered and the user will get an error message, rather than no response. Fork: https://github.com/GUI/jquery.iecors Related to: #174
- Loading branch information
Showing
1 changed file
with
79 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,79 @@ | ||
(function( jQuery ) { | ||
// Create the request object | ||
// (This is still attached to ajaxSettings for backward compatibility) | ||
jQuery.ajaxSettings.xdr = function() { | ||
return (window.XDomainRequest ? new window.XDomainRequest() : null); | ||
}; | ||
|
||
// Determine support properties | ||
(function( xdr ) { | ||
jQuery.extend( jQuery.support, { iecors: !!xdr }); | ||
})( jQuery.ajaxSettings.xdr() ); | ||
|
||
// Create transport if the browser can provide an xdr | ||
if ( jQuery.support.iecors ) { | ||
|
||
jQuery.ajaxTransport(function( s ) { | ||
var callback, | ||
xdr = s.xdr(); | ||
|
||
return { | ||
send: function( headers, complete ) { | ||
xdr.onload = function() { | ||
var headers = { 'Content-Type': xdr.contentType }; | ||
complete(200, 'OK', { text: xdr.responseText }, headers); | ||
}; | ||
|
||
// Apply custom fields if provided | ||
if ( s.xhrFields ) { | ||
xhr.onerror = s.xhrFields.error; | ||
xhr.ontimeout = s.xhrFields.timeout; | ||
} | ||
|
||
xdr.open( s.type, s.url ); | ||
|
||
// XDR has no method for setting headers O_o | ||
|
||
xdr.send( ( s.hasContent && s.data ) || null ); | ||
}, | ||
|
||
abort: function() { | ||
xdr.abort(); | ||
} | ||
}; | ||
}); | ||
} | ||
})( jQuery ); | ||
// Coded to Microsoft XDR spec: http://msdn.microsoft.com/en-us/library/cc288060(v=vs.85).aspx | ||
|
||
(function($) { | ||
"use strict"; | ||
|
||
// Create the request object | ||
// (This is still attached to ajaxSettings for backward compatibility) | ||
$.ajaxSettings.xdr = function() { | ||
return (window.XDomainRequest ? new window.XDomainRequest() : null); | ||
}; | ||
|
||
// Determine support properties | ||
(function(xdr) { | ||
$.extend($.support, { iecors: !!xdr }); | ||
})(window.XDomainRequest); | ||
|
||
// Create transport if the browser can provide an xdr (and fails $.support.cors) | ||
if ($.support.iecors) { | ||
$.ajaxTransport(function(options, originalOptions, jqXHR) { | ||
var xdr; | ||
|
||
return { | ||
send: function(headers, complete) { | ||
xdr = options.xdr(); | ||
|
||
// XDR does not support custom headers | ||
|
||
// Seems that xdr requests can get hung up indefinitely without a timeout. | ||
xdr.timeout = options.timeout || 10000; | ||
|
||
// IE9 has a bug that requires the xdr.onprogress method to be set. We'll just set them all, just in case. | ||
// (http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/30ef3add-767c-4436-b8a9-f1ca19b4812e) | ||
xdr.onload = xdr.onerror = xdr.ontimeout = xdr.onprogress = $.noop; | ||
|
||
xdr.onload = function() { | ||
var headers = { | ||
'Content-Type': xdr.contentType | ||
}; | ||
|
||
complete(200, 'OK', { text: xdr.responseText }, headers); | ||
}; | ||
|
||
if (options.xhrFields) { | ||
if (options.xhrFields.progress) xhr.onprogress = options.xhrFields.progress; | ||
if (options.xhrFields.error) xhr.onerror = options.xhrFields.error; | ||
if (options.xhrFields.timeout) xhr.ontimeout = options.xhrFields.timeout; | ||
// XDR does not support withCredentials | ||
} else { | ||
xdr.onprogress = function() { | ||
}; | ||
xdr.onerror = function() { | ||
complete(404, "Not Found"); | ||
}; | ||
xdr.ontimeout = function() { | ||
complete(408, "Request Timeout"); | ||
}; | ||
} | ||
|
||
// // TODO: If you're getting "Aborted" requests in IE9, try uncommenting this block. | ||
// // A few people reported 'jQuery.noop' wasn't good enough, but I can't figure out why. | ||
// xdr.onprogress = function() { | ||
// }; | ||
|
||
xdr.open(options.type, options.url); | ||
|
||
if (options.hasContent && options.data) { | ||
xdr.send(options.data); | ||
} else { | ||
xdr.send(); | ||
} | ||
}, | ||
|
||
abort: function () { | ||
return xdr && xdr.abort(); | ||
} | ||
}; | ||
}); | ||
} | ||
})(jQuery); |