Tuesday, August 4, 2009

Patch of jQuery.windowName.js to support XML content for cross domain ajax

By default, jquery cross domain ajax plugin jQuery.windowName.js is only support data type in json.
The trick to let it support xml, mock the xml object request header content-type is xml, and the following is the patch for jQuery.windowName.js, and it is depend on createXMLDocument plugin:

http://plugins.jquery.com/project/createXMLDocument

onreadystatechange = function(isTimeout) {
if (!requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout")) {
requestDone = true;
if (ival) {
clearInterval(ival);
ival = null;
}
status = isTimeout === 'timeout' && 'timeout' || !$.httpSuccess(xhr) && "error" || 'success';
if (status === 'success') {
try {
//Patched by Cyrus
// When the response is XML, the XML response is in xhr.responseText instead of
// xhr.responseXML. We think it is a bug in jQuery.windowName.js.
var ct = xhr.getResponseHeader("content-type");
var isXml = s.dataType == "xml" || !s.dataType && ct && ct.indexOf("xml") >= 0;
if (isXml) {
var xmlText = xhr.responseText;
// During server side XML serialization, in some cases (to be determined exactly where)
// we have a '?' in front of an xml response. Need to take it out.
if (xmlText.indexOf('?') == 1) {
xmlText = xmlText.substring(1);
}
//IE 8 cannot handle start with <?xml version="1.0" encoding="utf-8"?>. Need to take it out
if (xmlText.indexOf('<?xml version="1.0" encoding="utf-8"?>') == 1) {
xmlText = xmlText.substring('<?xml version="1.0" encoding="utf-8"?>'.length + 1);
}
xhr.responseXML = $.createXMLDocument(xmlText);
console.log('Mocking xml');
}
/////////////////////////
data = $.httpData(xhr, s.dataType, s.dataFilter);
} catch (er) {
status = 'parsererror';
}
}

No comments:

Post a Comment