From f3cfceea8fc0e68c7ec6da5971598822d957a4d6 Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Wed, 31 Jul 2013 21:32:45 +0200 Subject: [PATCH] Enable response parsing and converting (from text to JSON, e.g.) by using the code from jQuery 1.9.1 --- jquery.iecors.js | 63 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/jquery.iecors.js b/jquery.iecors.js index 73af026..caf5737 100644 --- a/jquery.iecors.js +++ b/jquery.iecors.js @@ -18,18 +18,69 @@ return { send: function( headers, complete ) { + // Get a new xdr var xdr = s.xdr(); xdr.onload = function() { - var headers = { 'Content-Type': xdr.contentType }; - complete(200, 'OK', { text: xdr.responseText }, headers); + var status, responseHeaders, statusText, responses; + + // Firefox throws exceptions when accessing properties + // of an xdr when a network error occurred + // http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE) + try { + + responses = {}; + // IE8 does not set status at all. + status = xdr.status || 200; + if (xdr.contentType) { + responseHeaders = { 'Content-Type': xdr.contentType }; + if (xdr.contentType.toLowerCase().indexOf("application/json") == 0) + s.dataTypes.push("json"); + } + + // When requesting binary data, IE6-9 will throw an exception + // on any attempt to access responseText (#11426) + if ( typeof xdr.responseText === "string" ) { + responses.text = xdr.responseText; + } + + // Firefox throws an exception when accessing + // statusText for faulty cross-domain requests + try { + statusText = xdr.statusText || "OK"; + } catch( e ) { + // We normalize with Webkit giving an empty statusText + status = 0; + statusText = ""; + } + + // Filter status for non standard behaviors + + // If the request is local and we have data: assume a success + // (success with no data won't get notified, that's the best we + // can do given current implementations) + if ( !status && s.isLocal && !s.crossDomain ) { + status = responses.text ? 200 : 404; + // IE - #1450: sometimes returns 1223 when it should be 204 + } else if ( status === 1223 ) { + status = 204; + } + + } catch( firefoxAccessException ) { + complete( -1, firefoxAccessException ); + } + + // Call complete if needed + if ( responses ) { + complete( status, statusText, responses, responseHeaders ); + } }; // Apply custom fields if provided - if ( s.xhrFields ) { - xhr.onerror = s.xhrFields.error; - xhr.ontimeout = s.xhrFields.timeout; - } + if ( s.xhrFields ) { + xdr.onerror = s.xhrFields.error; + xdr.ontimeout = s.xhrFields.timeout; + } xdr.open( s.type, s.url );