-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to v1.0.2, including some code cleanup
- Loading branch information
Jason Moon
committed
May 2, 2014
1 parent
f77fa04
commit 1a7ea05
Showing
2 changed files
with
107 additions
and
95 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,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; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
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(); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
}); | ||
|
||
})); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
There's a problem with your regular expression. It matches
getty
,lepost
, etc. since you're looking for a string that starts with "get" or ends with "post". I believe what you wanted was/^(?:get|post)$/i
.Enjoy!