From 1a7ea05b59ddf8f50c2a735ada2b36e72e09a6c2 Mon Sep 17 00:00:00 2001 From: Jason Moon Date: Fri, 2 May 2014 13:01:45 -0500 Subject: [PATCH] Update to v1.0.2, including some code cleanup --- jQuery.XDomainRequest.js | 196 +++++++++++++++++++---------------- jquery.xdomainrequest.min.js | 6 +- 2 files changed, 107 insertions(+), 95 deletions(-) diff --git a/jQuery.XDomainRequest.js b/jQuery.XDomainRequest.js index fdcfcfc..632d312 100644 --- a/jQuery.XDomainRequest.js +++ b/jQuery.XDomainRequest.js @@ -1,102 +1,114 @@ -// jQuery.XDomainRequest.js -// Author: Jason Moon - @JSONMOON -// IE8+ -(function (factory) { - if (typeof define === 'function' && define.amd) { - // AMD. Register as anonymous module. - define(['jquery'], factory); - } else { - // Browser globals. - factory(jQuery); - } -}(function ($) { +/*! + * jQuery-ajaxTransport-XDomainRequest - v1.0.2 - 2014-05-02 + * https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest + * Copyright (c) 2014 Jason Moon (@JSONMOON) + * Licensed MIT (/blob/master/LICENSE.txt) + */ +(function(factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as anonymous module. + define(['jquery'], factory); + } else { + // Browser globals. + factory(jQuery); + } +}(function($) { + +// Only continue if we're on IE8/IE9 with jQuery 1.5+ (contains the ajaxTransport function) +if ($.support.cors || !$.ajaxTransport || !window.XDomainRequest) { + return; +} -if (!$.support.cors && $.ajaxTransport && window.XDomainRequest) { - var httpRegEx = /^https?:\/\//i; - var getOrPostRegEx = /^get|post$/i; - var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i'); - var htmlRegEx = /text\/html/i; - var jsonRegEx = /\/json/i; - var xmlRegEx = /\/xml/i; +var httpRegEx = /^https?:\/\//i; +var getOrPostRegEx = /^get|post$/i; +var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i'); + +// ajaxTransport exists in jQuery 1.5+ +$.ajaxTransport('* text html xml json', function(options, userOptions, jqXHR) { - // ajaxTransport exists in jQuery 1.5+ - $.ajaxTransport('* text html xml json', function(options, userOptions, jqXHR){ - // XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page - if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(options.url) && sameSchemeRegEx.test(options.url)) { - var xdr = null; - var userType = (userOptions.dataType||'').toLowerCase(); - return { - send: function(headers, complete){ - xdr = new XDomainRequest(); - if (/^\d+$/.test(userOptions.timeout)) { - xdr.timeout = userOptions.timeout; - } - xdr.ontimeout = function(){ - complete(500, 'timeout'); - }; - xdr.onload = function(){ - var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType; - var status = { - code: 200, - message: 'success' - }; - var responses = { - text: xdr.responseText - }; + // Only continue if the request is: asynchronous, uses GET or POST method, has HTTP or HTTPS protocol, and has the same scheme as the calling page + if (!options.crossDomain || !options.async || !getOrPostRegEx.test(options.type) || !httpRegEx.test(options.url) || !sameSchemeRegEx.test(options.url)) { + return; + } + + var xdr = null; + + return { + send: function(headers, complete) { + var postData = ''; + var userType = (userOptions.dataType || '').toLowerCase(); + + xdr = new XDomainRequest(); + if (/^\d+$/.test(userOptions.timeout)) { + xdr.timeout = userOptions.timeout; + } + + xdr.ontimeout = function() { + complete(500, 'timeout'); + }; + + xdr.onload = function() { + var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType; + var status = { + code: 200, + message: 'success' + }; + var responses = { + text: xdr.responseText + }; + try { + if (userType === 'html' || /text\/html/i.test(xdr.contentType)) { + responses.html = xdr.responseText; + } else if (userType === 'json' || (userType !== 'text' && /\/json/i.test(xdr.contentType))) { try { - if (userType === 'html' || htmlRegEx.test(xdr.contentType)) { - responses.html = xdr.responseText; - } else if (userType === 'json' || (userType !== 'text' && jsonRegEx.test(xdr.contentType))) { - try { - responses.json = $.parseJSON(xdr.responseText); - } catch(e) { - status.code = 500; - status.message = 'parseerror'; - //throw 'Invalid JSON: ' + xdr.responseText; - } - } else if (userType === 'xml' || (userType !== 'text' && xmlRegEx.test(xdr.contentType))) { - var doc = new ActiveXObject('Microsoft.XMLDOM'); - doc.async = false; - try { - doc.loadXML(xdr.responseText); - } catch(e) { - doc = undefined; - } - if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) { - status.code = 500; - status.message = 'parseerror'; - throw 'Invalid XML: ' + xdr.responseText; - } - responses.xml = doc; - } - } catch(parseMessage) { - throw parseMessage; - } finally { - complete(status.code, status.message, responses, allResponseHeaders); + responses.json = $.parseJSON(xdr.responseText); + } catch(e) { + status.code = 500; + status.message = 'parseerror'; + //throw 'Invalid JSON: ' + xdr.responseText; } - }; - // set an empty handler for 'onprogress' so requests don't get aborted - xdr.onprogress = function(){}; - xdr.onerror = function(){ - complete(500, 'error', { - text: xdr.responseText - }); - }; - var postData = ''; - if (userOptions.data) { - postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data); - } - xdr.open(options.type, options.url); - xdr.send(postData); - }, - abort: function(){ - if (xdr) { - xdr.abort(); + } else if (userType === 'xml' || (userType !== 'text' && /\/xml/i.test(xdr.contentType))) { + var doc = new ActiveXObject('Microsoft.XMLDOM'); + doc.async = false; + try { + doc.loadXML(xdr.responseText); + } catch(e) { + doc = undefined; + } + if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) { + status.code = 500; + status.message = 'parseerror'; + throw 'Invalid XML: ' + xdr.responseText; + } + responses.xml = doc; } + } catch(parseMessage) { + throw parseMessage; + } finally { + complete(status.code, status.message, responses, allResponseHeaders); } }; + + // set an empty handler for 'onprogress' so requests don't get aborted + xdr.onprogress = function(){}; + xdr.onerror = function() { + complete(500, 'error', { + text: xdr.responseText + }); + }; + + if (userOptions.data) { + postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data); + } + xdr.open(options.type, options.url); + xdr.send(postData); + }, + abort: function() { + if (xdr) { + xdr.abort(); + } } - }); -} + }; +}); })); \ No newline at end of file diff --git a/jquery.xdomainrequest.min.js b/jquery.xdomainrequest.min.js index 089193f..cb4c734 100644 --- a/jquery.xdomainrequest.min.js +++ b/jquery.xdomainrequest.min.js @@ -1,7 +1,7 @@ /*! - * jQuery-ajaxTransport-XDomainRequest - v1.0.1 - 2013-10-17 + * jQuery-ajaxTransport-XDomainRequest - v1.0.2 - 2014-05-02 * https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest - * Copyright (c) 2013 Jason Moon (@JSONMOON) + * Copyright (c) 2014 Jason Moon (@JSONMOON) * Licensed MIT (/blob/master/LICENSE.txt) */ -(function($){if(!$.support.cors&&$.ajaxTransport&&window.XDomainRequest){var n=/^https?:\/\//i;var o=/^get|post$/i;var p=new RegExp('^'+location.protocol,'i');var q=/text\/html/i;var r=/\/json/i;var s=/\/xml/i;$.ajaxTransport('* text html xml json',function(i,j,k){if(i.crossDomain&&i.async&&o.test(i.type)&&n.test(i.url)&&p.test(i.url)){var l=null;var m=(j.dataType||'').toLowerCase();return{send:function(f,g){l=new XDomainRequest();if(/^\d+$/.test(j.timeout)){l.timeout=j.timeout}l.ontimeout=function(){g(500,'timeout')};l.onload=function(){var a='Content-Length: '+l.responseText.length+'\r\nContent-Type: '+l.contentType;var b={code:200,message:'success'};var c={text:l.responseText};try{if(m==='html'||q.test(l.contentType)){c.html=l.responseText}else if(m==='json'||(m!=='text'&&r.test(l.contentType))){try{c.json=$.parseJSON(l.responseText)}catch(e){b.code=500;b.message='parseerror'}}else if(m==='xml'||(m!=='text'&&s.test(l.contentType))){var d=new ActiveXObject('Microsoft.XMLDOM');d.async=false;try{d.loadXML(l.responseText)}catch(e){d=undefined}if(!d||!d.documentElement||d.getElementsByTagName('parsererror').length){b.code=500;b.message='parseerror';throw'Invalid XML: '+l.responseText;}c.xml=d}}catch(parseMessage){throw parseMessage;}finally{g(b.code,b.message,c,a)}};l.onprogress=function(){};l.onerror=function(){g(500,'error',{text:l.responseText})};var h='';if(j.data){h=($.type(j.data)==='string')?j.data:$.param(j.data)}l.open(i.type,i.url);l.send(h)},abort:function(){if(l){l.abort()}}}}})}})(jQuery); \ No newline at end of file +(function(a){if(typeof define==='function'&&define.amd){define(['jquery'],a)}else{a(jQuery)}}(function($){if($.support.cors||!$.ajaxTransport||!window.XDomainRequest){return}var n=/^https?:\/\//i;var o=/^get|post$/i;var p=new RegExp('^'+location.protocol,'i');$.ajaxTransport('* text html xml json',function(j,k,l){if(!j.crossDomain||!j.async||!o.test(j.type)||!n.test(j.url)||!p.test(j.url)){return}var m=null;return{send:function(f,g){var h='';var i=(k.dataType||'').toLowerCase();m=new XDomainRequest();if(/^\d+$/.test(k.timeout)){m.timeout=k.timeout}m.ontimeout=function(){g(500,'timeout')};m.onload=function(){var a='Content-Length: '+m.responseText.length+'\r\nContent-Type: '+m.contentType;var b={code:200,message:'success'};var c={text:m.responseText};try{if(i==='html'||/text\/html/i.test(m.contentType)){c.html=m.responseText}else if(i==='json'||(i!=='text'&&/\/json/i.test(m.contentType))){try{c.json=$.parseJSON(m.responseText)}catch(e){b.code=500;b.message='parseerror'}}else if(i==='xml'||(i!=='text'&&/\/xml/i.test(m.contentType))){var d=new ActiveXObject('Microsoft.XMLDOM');d.async=false;try{d.loadXML(m.responseText)}catch(e){d=undefined}if(!d||!d.documentElement||d.getElementsByTagName('parsererror').length){b.code=500;b.message='parseerror';throw'Invalid XML: '+m.responseText;}c.xml=d}}catch(parseMessage){throw parseMessage;}finally{g(b.code,b.message,c,a)}};m.onprogress=function(){};m.onerror=function(){g(500,'error',{text:m.responseText})};if(k.data){h=($.type(k.data)==='string')?k.data:$.param(k.data)}m.open(j.type,j.url);m.send(h)},abort:function(){if(m){m.abort()}}}})})); \ No newline at end of file