gpt4 book ai didi

internet-explorer - IE跨域ajax调用XDomainRequest错误

转载 作者:行者123 更新时间:2023-12-01 04:58:23 33 4
gpt4 key购买 nike

我必须使用以下代码从 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 中按以下顺序收到更改框:“发送前”、“XDR onerror”、“发送后”。

我在服务器端有以下代码

@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 onerror”。我应该添加什么才能在 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);

要删除,请执行以下操作: document.body.removeChild( document.getElementById("myFrameId") );或者 root.removeChild(myFrame);

关于internet-explorer - IE跨域ajax调用XDomainRequest错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12588058/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com