source: [view]
var plainXhr = getPlainXhr();
if(csXhrSupport === undefined && window.XMLHttpRequest){
// just run this once to see if we have cross-site support
try{
var xhr = new XMLHttpRequest();
xhr.open("GET","http://testing-cross-domain-capability.com",true);
csXhrSupport = true;
dojo.config.noRequestedWithHeaders = true;
}catch(e){
csXhrSupport = false;
}
}
dojox.io.xhrPlugins.register(
"cs-xhr",
function(method,args){
return (csXhrSupport ||
(window.XDomainRequest && args.sync !== true &&
(method == "GET" || method == "POST" || httpAdapter))) &&
(args.url.substring(0,url.length) == url);
},
csXhrSupport ? plainXhr : function(){
var normalXhrObj = dojo._xhrObj;
// we will just substitute this in temporarily so we can use XDomainRequest instead of XMLHttpRequest
dojo._xhrObj = function(){
var xdr = new XDomainRequest();
xdr.readyState = 1;
xdr.setRequestHeader = function(){}; // just absorb them, we can't set headers :/
xdr.getResponseHeader = function(header){ // this is the only header we can access
return header == "Content-Type" ? xdr.contentType : null;
}
// adapt the xdr handlers to xhr
function handler(status, readyState){
return function(){
xdr.readyState = readyState;
xdr.status = status;
}
}
xdr.onload = handler(200, 4);
xdr.onprogress = handler(200, 3);
xdr.onerror = handler(404, 4); // an error, who knows what the real status is
return xdr;
};
var dfd = (httpAdapter ? httpAdapter(getPlainXhr()) : getPlainXhr()).apply(dojo,arguments);
dojo._xhrObj = normalXhrObj;
return dfd;
}
);