我使用以下代码在 IE 中使用 xDomain Request 发出跨域 jquery ajax 请求。
var xdr;
var URL=parentDomain+"/chat/getmessagesservlet";
if (jQuery.browser.msie && window.XDomainRequest) {
// Use Microsoft XDR
var xdr = new XDomainRequest();
xdr.open("POST", URL + '?to='+chatboxtitle+'&yourName='+myName+'&chatBoxIndex='+tabIncr);
xdr.onload = function () {
var dom = new ActiveXObject("Microsoft.XMLDOM");
dom.async = false;
JSON = $.parseJSON(xdr.responseText);
if (JSON == null || typeof (JSON) == 'undefined') {
JSON = $.parseJSON(data.firstChild.textContent);
}
processData(JSON); // internal function
};
xdr.onerror = function() {
_result = false;
};
xdr.send();
} else {
$.ajax({
type: 'POST',
url: URL,
processData: true,
data: {'to':chatboxtitle,'yourName':myName,'chatBoxIndex':tabIncr},
dataType: "json",
async: false,
success: function (data) {
processData(data);
}
});
}'
我也在服务器端设置标题如下:
response.setHeader("Access-Control-Allow-Origin","*");
response.setHeader("Content-Type","text/plain");
response.setHeader("Access-Control-Allow-Methods"," GET, POST");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
但在 IE8 和 IE9 中它不起作用。ResponseText 返回 null 或为空。我已经看到使用 jQuery
.ajaxTransport( dataType, handler(options, originalOptions, jqXHR)
或者
.ajaxPrefilter( [dataTypes ], handler(options, originalOptions, jqXHR) )
可以进行跨域请求。但我不知道如何在我的代码中使用它。选项和 originalOptions 参数中要传递的值是什么?请给我指导来解决这个问题。