Skip to content

Commit 8754607

Browse files
committed
Initial commit
1 parent 69ba582 commit 8754607

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

jQuery.XDomainRequest.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// jQuery.XDomainRequest.js
2+
// Author: Jason Moon - @JSONMOON
3+
// IE8+
4+
if (!jQuery.support.cors && window.XDomainRequest) {
5+
var httpRegEx = /^https?:\/\//i;
6+
var getOrPostRegEx = /^get|post$/i;
7+
var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
8+
var xmlRegEx = /\/xml/i;
9+
10+
// ajaxTransport exists in jQuery 1.5+
11+
jQuery.ajaxTransport('text html xml json', function(options, userOptions, jqXHR){
12+
// XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
13+
if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(userOptions.url) && sameSchemeRegEx.test(userOptions.url)) {
14+
var xdr = null;
15+
var userType = (userOptions.dataType||'').toLowerCase();
16+
return {
17+
send: function(headers, complete){
18+
xdr = new XDomainRequest();
19+
if (/^\d+$/.test(userOptions.timeout)) {
20+
xdr.timeout = userOptions.timeout;
21+
}
22+
xdr.ontimeout = function(){
23+
complete(500, 'timeout');
24+
};
25+
xdr.onload = function(){
26+
var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
27+
var status = {
28+
code: 200,
29+
message: 'success'
30+
};
31+
var responses = {
32+
text: xdr.responseText
33+
};
34+
/*
35+
if (userType === 'html') {
36+
responses.html = xdr.responseText;
37+
} else
38+
*/
39+
try {
40+
if (userType === 'json') {
41+
try {
42+
responses.json = JSON.parse(xdr.responseText);
43+
} catch(e) {
44+
status.code = 500;
45+
status.message = 'parseerror';
46+
//throw 'Invalid JSON: ' + xdr.responseText;
47+
}
48+
} else if ((userType === 'xml') || ((userType !== 'text') && xmlRegEx.test(xdr.contentType))) {
49+
var doc = new ActiveXObject('Microsoft.XMLDOM');
50+
doc.async = false;
51+
try {
52+
doc.loadXML(xdr.responseText);
53+
} catch(e) {
54+
doc = undefined;
55+
}
56+
if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
57+
status.code = 500;
58+
status.message = 'parseerror';
59+
throw 'Invalid XML: ' + xdr.responseText;
60+
}
61+
responses.xml = doc;
62+
}
63+
} catch(parseMessage) {
64+
throw parseMessage;
65+
} finally {
66+
complete(status.code, status.message, responses, allResponseHeaders);
67+
}
68+
};
69+
xdr.onerror = function(){
70+
complete(500, 'error', {
71+
text: xdr.responseText
72+
});
73+
};
74+
xdr.open(options.type, options.url);
75+
//xdr.send(userOptions.data);
76+
xdr.send();
77+
},
78+
abort: function(){
79+
if (xdr) {
80+
xdr.abort();
81+
}
82+
}
83+
};
84+
}
85+
});
86+
}

0 commit comments

Comments
 (0)