views:

168

answers:

1

I am trying to do "POST" with XMLHttpRequest in a firefox extension, and after that I try to get the "Location" header and open it in a new tab. For some reason, the XMLHttpRequest doesn't contain a location header.

My code

function web(url,request)
{
    var http = new XMLHttpRequest();
    http.open('POST',url,true);
    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    http.onreadystatechange=function() {
    if(http.readyState == 2) {
        alert(http.getResponseHeader("Location"));
        }
    }
    http.send(request);
}

Also, if I change the alert to getAllResponseHeaders() to see all headers, I just don't see the location header there. If I try to spy on the request of the original site with Firebug, it does show me the location header in the response. Please help me resolve my issues. Thanks :)

P.S. I'm also unable to open a link in new tab using window.open(url, this.window.title);, but as that's not directly related to the rest of this I'll post a separate question to ask about it.

+1  A: 

I think that current implementations of XHR are Location: agnostic. In other words, since XHR transparently follows HTTP redirects in a manner that is invisible to your code, I suspect that the whole XHR ball of wax has been made Location: agnostic and that the header is simply being stripped out.

[edit] Actually, now that I think of it, if XHR is following the redirect, then wouldn't it be the headers of the response to the redirect that you're seeing, and not the headers of the redirect itself?

greim
Sorry but I don't really understand what "Location: agnostic" means..If it means that I simply can't get it in any way from XHR, is there any other way I can do what I want?
Michael S.
I mean that the API that XHR exposes to programmers has no concept of following Location: redirects. It is handled magically behind the curtain. Also see the [edit] I just added to the answer.
greim