Skip to content

Commit 08bc455

Browse files
weikinhuangkswedberg
authored andcommitted
iterate through array with $.each. Closes jquery#258
1 parent 56e8320 commit 08bc455

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

entries/jQuery.ajax.xml

+24-18
Original file line numberDiff line numberDiff line change
@@ -284,29 +284,35 @@ jqxhr.always(function() { alert("second complete"); });</code></pre>
284284
<p><strong>At present</strong>, due to a bug in Firefox where <code>.getAllResponseHeaders()</code> returns the empty string although <code>.getResponseHeader('Content-Type')</code> returns a non-empty string, automatically decoding JSON CORS responses in Firefox with jQuery is not supported.</p>
285285
<p>A workaround to this is possible by overriding <code>jQuery.ajaxSettings.xhr</code> as follows:</p>
286286
<pre><code>
287-
var _super = jQuery.ajaxSettings.xhr;
288-
jQuery.ajaxSettings.xhr = function () {
289-
var xhr = _super(),
290-
getAllResponseHeaders = xhr.getAllResponseHeaders;
287+
(function () {
288+
var _super = jQuery.ajaxSettings.xhr,
289+
xhrCorsHeaders = [ "Cache-Control", "Content-Language", "Content-Type", "Expires", "Last-Modified", "Pragma" ];
291290

292-
xhr.getAllResponseHeaders = function () {
293-
if ( getAllResponseHeaders() ) {
294-
return getAllResponseHeaders();
295-
}
296-
var allHeaders = "";
291+
jQuery.ajaxSettings.xhr = function () {
292+
var xhr = _super(),
293+
getAllResponseHeaders = xhr.getAllResponseHeaders;
297294

298-
$( ["Cache-Control", "Content-Language", "Content-Type",
299-
"Expires", "Last-Modified", "Pragma"] )
300-
.each(function (i, header_name) {
301-
if ( xhr.getResponseHeader( header_name ) ) {
302-
allHeaders += header_name + ": " + xhr.getResponseHeader( header_name ) + "\n";
295+
xhr.getAllResponseHeaders = function () {
296+
var allHeaders = "";
297+
try {
298+
allHeaders = getAllResponseHeaders.apply( xhr );
299+
if ( allHeaders ) {
300+
return allHeaders;
301+
}
302+
} catch ( e ) {
303303
}
304+
305+
$.each( xhrCorsHeaders, function ( i, headerName ) {
306+
if ( xhr.getResponseHeader( headerName ) ) {
307+
allHeaders += headerName + ": " + xhr.getResponseHeader( headerName ) + "\n";
308+
}
309+
});
304310
return allHeaders;
305-
});
306-
};
311+
};
307312

308-
return xhr;
309-
};
313+
return xhr;
314+
};
315+
})();
310316
</code></pre>
311317

312318
<h4 id="extending-ajax">Extending Ajax</h4>

0 commit comments

Comments
 (0)