2

I have to use the following code to logout from a page in an 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);

}); 

However, I get alter boxes in IE8 in the this order: "before send", "XDR onerror", "after send".

I have the following code in server side

@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);
}

I can verify that the logout works perfect for Firefox after I use $.getJSON and response.setHeader("Access-Control-Allow-Origin", "*") and so on; however it doesn't logout when I use IE.

Then I found this comment online:

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

So I added XDomainRequest. However, I got my alert dialog "XDR onerror" in IE8. What should I add to make this cross domain call in IE?

wlin
  • 75
  • 1
  • 3
  • 10

2 Answers2

1

try to use iframe to make a XDomainGet request:

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);

To remove do: document.body.removeChild( document.getElementById("myFrameId") ); or root.removeChild(myFrame);

Diego Sousa
  • 81
  • 1
  • 4
0

CORS requests in IE8/9 can be enabled with jQuery plugin jquery-transport-xdr

Gordon Freeman
  • 891
  • 7
  • 5