Skip to content

Instantly share code, notes, and snippets.

@evanportwood
Created October 8, 2013 18:40
Show Gist options
  • Save evanportwood/6889446 to your computer and use it in GitHub Desktop.
Save evanportwood/6889446 to your computer and use it in GitHub Desktop.
create xdr request of cors is not supported
if (!jQuery.support.cors && window.XDomainRequest) {
var httpRegEx = /^https?:\/\//i,
getOrPostRegEx = /^get|post$/i,
sameSchemeRegEx = RegExp("^" + location.protocol, "i"),
xmlRegEx = /\/xml/i;
jQuery.ajaxTransport("text html xml json", function (e, t) {
if (e.crossDomain && e.async && getOrPostRegEx.test(e.type) && httpRegEx.test(t.url) && sameSchemeRegEx.test(t.url)) {
var n = null,
i = (t.dataType || "").toLowerCase();
return {
send: function (r, a) {
n = new XDomainRequest, /^\d+$/.test(t.timeout) && (n.timeout = t.timeout), n.ontimeout = function () {
a(500, "timeout")
}, n.onload = function () {
var e = "Content-Length: " + n.responseText.length + "\r\nContent-Type: " + n.contentType,
t = {
code: 200,
message: "success"
}, r = {
text: n.responseText
};
try {
if ("json" === i) try {
r.json = JSON.parse(n.responseText)
} catch (o) {
t.code = 500, t.message = "parseerror"
} else if ("xml" === i || "text" !== i && xmlRegEx.test(n.contentType)) {
var s = new ActiveXObject("Microsoft.XMLDOM");
s.async = !1;
try {
s.loadXML(n.responseText)
} catch (o) {
s = void 0
}
if (!s || !s.documentElement || s.getElementsByTagName("parsererror").length) throw t.code = 500, t.message = "parseerror", "Invalid XML: " + n.responseText;
r.xml = s
}
} catch (l) {
throw l
} finally {
a(t.code, t.message, r, e)
}
}, n.onerror = function () {
a(500, "error", {
text: n.responseText
})
}, n.open(e.type, e.url), n.send()
},
abort: function () {
n && n.abort()
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment