IE跨域ajax调用XDomainRequest错误



我必须使用以下代码从Iframe 中的页面注销

//The javascript code is in a page in my-domain
//it tries to logout from the page in other-domain
$(window).unload(function() 
{
if ($.browser.msie && window.XDomainRequest) // IE
{
var xdr = new XDomainRequest();
if (xdr)
{
xdr.onerror = function(){alert("XDR onerror");};
xdr.ontimeout   = function(){alert("XDR timeout");};
xdr.onprogress  = function(){alert("XDR onprogress");};
xdr.onload      = function(){alert("XDR onload");};
xdr.timeout     = 5000;
xdr.open("GET", 'http://other-domain/.../j_spring_security_logout');
alert("before send");
xdr.send();
alert("after send");
}
else
alert('Failed to create new XDR object.');
} 
else // firefox
$.getJSON( 'http://other-domain/.../j_spring_security_logout?callback=?', null);
}); 

然而,我在IE8中得到的alter框的顺序是:"发送之前"、"XDR oneror"、"发送之后"。

我在服务器端中有以下代码

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
response.setHeader("Access-Control-Max-Age", "360");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}

在使用$.getJSON和response.setHeader("Access Control Allow Origin","*")等之后,我可以验证注销是否适用于Firefox;然而,当我使用IE.时,它不会注销

然后我在网上发现了这样的评论:

"For cross-domain, IE requires you to use XDomainRequest instead of XMLHttpRequest.
jQuery doesn't do it so you have to do it manually:"

所以我添加了XDomainRequest。然而,我在IE8中得到了警报对话框"XDR oneror"。我应该添加什么来在IE中进行跨域调用?

尝试使用iframe发出XDomainGet请求:

root = document.body;
myIframe = document.createElement("iframe");
myframe.setAttribute("id", "myFrameId")
myIframe.setAttribute("style", "position: absolute; top: -100px;");
//Making a xget
myIframe.setAttribute("src", "http://other-domain.../j_spring_security_logout?callback=?");
root.appendChild(myframe);

要删除do:document.body.removeChild(document.getElementById("myFrameId"));或root.removeChild(myFrame);

CORSIE8/9中的请求可以使用jQuery插件jQuery transport xdr 启用

相关内容

  • 没有找到相关文章

最新更新