Skip to content

Commit 1a7ea05

Browse files
author
Jason Moon
committed
Update to v1.0.2, including some code cleanup
1 parent f77fa04 commit 1a7ea05

File tree

2 files changed

+107
-95
lines changed

2 files changed

+107
-95
lines changed

jQuery.XDomainRequest.js

Lines changed: 104 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,114 @@
1-
// jQuery.XDomainRequest.js
2-
// Author: Jason Moon - @JSONMOON
3-
// IE8+
4-
(function (factory) {
5-
if (typeof define === 'function' && define.amd) {
6-
// AMD. Register as anonymous module.
7-
define(['jquery'], factory);
8-
} else {
9-
// Browser globals.
10-
factory(jQuery);
11-
}
12-
}(function ($) {
1+
/*!
2+
* jQuery-ajaxTransport-XDomainRequest - v1.0.2 - 2014-05-02
3+
* https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest
4+
* Copyright (c) 2014 Jason Moon (@JSONMOON)
5+
* Licensed MIT (/blob/master/LICENSE.txt)
6+
*/
7+
(function(factory) {
8+
if (typeof define === 'function' && define.amd) {
9+
// AMD. Register as anonymous module.
10+
define(['jquery'], factory);
11+
} else {
12+
// Browser globals.
13+
factory(jQuery);
14+
}
15+
}(function($) {
16+
17+
// Only continue if we're on IE8/IE9 with jQuery 1.5+ (contains the ajaxTransport function)
18+
if ($.support.cors || !$.ajaxTransport || !window.XDomainRequest) {
19+
return;
20+
}
1321

14-
if (!$.support.cors && $.ajaxTransport && window.XDomainRequest) {
15-
var httpRegEx = /^https?:\/\//i;
16-
var getOrPostRegEx = /^get|post$/i;
17-
var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
18-
var htmlRegEx = /text\/html/i;
19-
var jsonRegEx = /\/json/i;
20-
var xmlRegEx = /\/xml/i;
22+
var httpRegEx = /^https?:\/\//i;
23+
var getOrPostRegEx = /^get|post$/i;
24+
var sameSchemeRegEx = new RegExp('^'+location.protocol, 'i');
25+
26+
// ajaxTransport exists in jQuery 1.5+
27+
$.ajaxTransport('* text html xml json', function(options, userOptions, jqXHR) {
2128

22-
// ajaxTransport exists in jQuery 1.5+
23-
$.ajaxTransport('* text html xml json', function(options, userOptions, jqXHR){
24-
// XDomainRequests must be: asynchronous, GET or POST methods, HTTP or HTTPS protocol, and same scheme as calling page
25-
if (options.crossDomain && options.async && getOrPostRegEx.test(options.type) && httpRegEx.test(options.url) && sameSchemeRegEx.test(options.url)) {
26-
var xdr = null;
27-
var userType = (userOptions.dataType||'').toLowerCase();
28-
return {
29-
send: function(headers, complete){
30-
xdr = new XDomainRequest();
31-
if (/^\d+$/.test(userOptions.timeout)) {
32-
xdr.timeout = userOptions.timeout;
33-
}
34-
xdr.ontimeout = function(){
35-
complete(500, 'timeout');
36-
};
37-
xdr.onload = function(){
38-
var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
39-
var status = {
40-
code: 200,
41-
message: 'success'
42-
};
43-
var responses = {
44-
text: xdr.responseText
45-
};
29+
// 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
30+
if (!options.crossDomain || !options.async || !getOrPostRegEx.test(options.type) || !httpRegEx.test(options.url) || !sameSchemeRegEx.test(options.url)) {
31+
return;
32+
}
33+
34+
var xdr = null;
35+
36+
return {
37+
send: function(headers, complete) {
38+
var postData = '';
39+
var userType = (userOptions.dataType || '').toLowerCase();
40+
41+
xdr = new XDomainRequest();
42+
if (/^\d+$/.test(userOptions.timeout)) {
43+
xdr.timeout = userOptions.timeout;
44+
}
45+
46+
xdr.ontimeout = function() {
47+
complete(500, 'timeout');
48+
};
49+
50+
xdr.onload = function() {
51+
var allResponseHeaders = 'Content-Length: ' + xdr.responseText.length + '\r\nContent-Type: ' + xdr.contentType;
52+
var status = {
53+
code: 200,
54+
message: 'success'
55+
};
56+
var responses = {
57+
text: xdr.responseText
58+
};
59+
try {
60+
if (userType === 'html' || /text\/html/i.test(xdr.contentType)) {
61+
responses.html = xdr.responseText;
62+
} else if (userType === 'json' || (userType !== 'text' && /\/json/i.test(xdr.contentType))) {
4663
try {
47-
if (userType === 'html' || htmlRegEx.test(xdr.contentType)) {
48-
responses.html = xdr.responseText;
49-
} else if (userType === 'json' || (userType !== 'text' && jsonRegEx.test(xdr.contentType))) {
50-
try {
51-
responses.json = $.parseJSON(xdr.responseText);
52-
} catch(e) {
53-
status.code = 500;
54-
status.message = 'parseerror';
55-
//throw 'Invalid JSON: ' + xdr.responseText;
56-
}
57-
} else if (userType === 'xml' || (userType !== 'text' && xmlRegEx.test(xdr.contentType))) {
58-
var doc = new ActiveXObject('Microsoft.XMLDOM');
59-
doc.async = false;
60-
try {
61-
doc.loadXML(xdr.responseText);
62-
} catch(e) {
63-
doc = undefined;
64-
}
65-
if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
66-
status.code = 500;
67-
status.message = 'parseerror';
68-
throw 'Invalid XML: ' + xdr.responseText;
69-
}
70-
responses.xml = doc;
71-
}
72-
} catch(parseMessage) {
73-
throw parseMessage;
74-
} finally {
75-
complete(status.code, status.message, responses, allResponseHeaders);
64+
responses.json = $.parseJSON(xdr.responseText);
65+
} catch(e) {
66+
status.code = 500;
67+
status.message = 'parseerror';
68+
//throw 'Invalid JSON: ' + xdr.responseText;
7669
}
77-
};
78-
// set an empty handler for 'onprogress' so requests don't get aborted
79-
xdr.onprogress = function(){};
80-
xdr.onerror = function(){
81-
complete(500, 'error', {
82-
text: xdr.responseText
83-
});
84-
};
85-
var postData = '';
86-
if (userOptions.data) {
87-
postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data);
88-
}
89-
xdr.open(options.type, options.url);
90-
xdr.send(postData);
91-
},
92-
abort: function(){
93-
if (xdr) {
94-
xdr.abort();
70+
} else if (userType === 'xml' || (userType !== 'text' && /\/xml/i.test(xdr.contentType))) {
71+
var doc = new ActiveXObject('Microsoft.XMLDOM');
72+
doc.async = false;
73+
try {
74+
doc.loadXML(xdr.responseText);
75+
} catch(e) {
76+
doc = undefined;
77+
}
78+
if (!doc || !doc.documentElement || doc.getElementsByTagName('parsererror').length) {
79+
status.code = 500;
80+
status.message = 'parseerror';
81+
throw 'Invalid XML: ' + xdr.responseText;
82+
}
83+
responses.xml = doc;
9584
}
85+
} catch(parseMessage) {
86+
throw parseMessage;
87+
} finally {
88+
complete(status.code, status.message, responses, allResponseHeaders);
9689
}
9790
};
91+
92+
// set an empty handler for 'onprogress' so requests don't get aborted
93+
xdr.onprogress = function(){};
94+
xdr.onerror = function() {
95+
complete(500, 'error', {
96+
text: xdr.responseText
97+
});
98+
};
99+
100+
if (userOptions.data) {
101+
postData = ($.type(userOptions.data) === 'string') ? userOptions.data : $.param(userOptions.data);
102+
}
103+
xdr.open(options.type, options.url);
104+
xdr.send(postData);
105+
},
106+
abort: function() {
107+
if (xdr) {
108+
xdr.abort();
109+
}
98110
}
99-
});
100-
}
111+
};
112+
});
101113

102114
}));

jquery.xdomainrequest.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)