From 5fa9a61d2e9226cb5850dd625ba86dbb0263100b Mon Sep 17 00:00:00 2001 From: Nick Muerdter Date: Sat, 17 Jan 2015 10:08:20 -0700 Subject: [PATCH] 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: https://github.com/18F/api.data.gov/issues/174 --- .../javascripts/_vendor/jquery.iecors.js | 125 +++++++++++------- 1 file changed, 79 insertions(+), 46 deletions(-) diff --git a/source/static/javascripts/_vendor/jquery.iecors.js b/source/static/javascripts/_vendor/jquery.iecors.js index 3c9d418a..c9cb6119 100644 --- a/source/static/javascripts/_vendor/jquery.iecors.js +++ b/source/static/javascripts/_vendor/jquery.iecors.js @@ -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);