开发者

Get jQuery post redirect response

开发者 https://www.devze.com 2022-12-23 18:44 出处:网络
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 usef

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 recei开发者_开发知识库ve "[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!


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">


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


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");
}
//...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号