views:

538

answers:

3

I've written some HTML/Javascript that sits on a third-party server for security reasons. This page performs a javascript post to another page on the same site. However, instead of responding with useful data, it instead wants to perform a redirect (if you would post via a normal HTML form to this page, it would redirect your browser). How can I process this process? I basically want to be able to extract the url's query parameters that it is trying to redirect with (and then put this link into a hidden form field).

Here is my basic ajax post...

$.ajax({
    url: '/someurl/idontcontrol', 
    data: serialized_form_data,
    async: false,
    type: 'POST',
    success: function(data, textStatus, x) {        
        alert(x);
        alert(x.getAllResponseHeaders());
        return false;
        $('#redirect_link').val(WHAT_DO_I_PUT_HERE);
    }
});

Note that the URL I am posting to is not one that I control, so I have no power over what it returns.

UPDATE: When I use the above alerts, I receive "[object XMLHttpRequest]" and "null". I'm monitoring the headers with a Firefox plugin and they seem be coming back as expected, but I can't seem to access them via javascript (I've also tried x.getResponseHeader('Location'), but that and all other calls to getResponseHeader return empty).

ALSO: I don't know if it matters, but the status code is 302 (as opposed to 301 or 303).

Thanks!

+1  A: 

You can't get the full HTTP headers from an AJAX call in JQUery, so you can't process the redirect in this way.

However with a raw javascript request you do have access to the XMLHttpRequest getAllResponseHeaders() method which will allow you to process the redirect (this function for single headers).

Andy
I'm supposed to be able to get the XMLHttpRequest object in my success callback function (see Greg S's post), but it doesn't seem to be getting populated correctly so my calls to either of those two methods is unsuccessful.
Wickethewok
+1  A: 

According to the jQuery Documentation the success method can take a third argument which is the XMLHttpRequest object. According to Mozilla's XMLHttpRequest page, this object should have a "status" property. If you check the value of this property, and it is a redirect code, such as 301 (permanent redirect) or 303 (temporary redirect) it is known the processing page is trying to perform a redirect. The "statusText" property should be able to be used to determine the location it is trying to redirect you to. If you know it is trying to redirect, you can then re-post the data through Ajax to the new URL.

The strange thing is though, when researching this, stumbled across this page that indicates the XMLHttpRequest object should follow redirects (see the comments). So it seems like this would be a non-issue.

Unless the processing page is using an HTML meta redirect (like below) to do the redirection. In that case, I'm not sure what could be done - maybe try to parse the returned HTML for meta tags and see if any of them are attempting a redirect.

<meta http-equiv="refresh" content="0;url=http://www.example.com/some-redirected-page"&gt;
Greg S
I added a third parameter to the success callback function; however, when I try to call x.getAllResponseHeaders(), it returns null.
Wickethewok
statusText and status should just be properties of the XMLHttpRequest object, getAllResponseHeaders may not need to be called. Does something like alert(x.statusText) or alert(x.status) work?
Greg S
did you find the reason why `getAllResponseHeaders()` returns `null`?
Gregory Pakosz
A: 

Sorry, not directly an answer to your question, but I'm sure it's possible with jQuery too as it's quite simple with Prototype.

// Warning: this is Prototype, not jQuery ;-)
//...
onComplete: function(response) {
    var refresh = response.getResponseHeader("Refresh");
    var whatever = response.getResponseHeader("Whatever");
}
//...
Nic
Is "response" just an XMLHttpRequest? Because that is what I'm getting back in jQuery, it just isn't being populated correctly...
Wickethewok