|
2 | 2 | // Author: Jason Moon - @JSONMOON
|
3 | 3 | // IE8+
|
4 | 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 = jQuery.parseJSON(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||null); |
76 |
| - }, |
77 |
| - abort: function(){ |
78 |
| - if (xdr) { |
79 |
| - xdr.abort(); |
80 |
| - } |
81 |
| - } |
82 |
| - }; |
83 |
| - } |
84 |
| - }); |
| 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 = jQuery.parseJSON(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||null); |
| 76 | + }, |
| 77 | + abort: function(){ |
| 78 | + if (xdr) { |
| 79 | + xdr.abort(); |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | + } |
| 84 | + }); |
85 | 85 | }
|
0 commit comments