开发者

ajax request via jquery for a url that redirects

I m trying to access a data after invoking a URL which redirects the output to another page with query strings.

ie:

 $.ajax({
        url: 'http://foo.com/results/bar.aspx?fooid = 123&more=1',
        success: function(data) {
            alert('Load was performed.'+data);
        }
    });

Reponse results empty. This URL is a redirect to another page with query string, I already have a page that parses the query string and write the output to a page.

But开发者_如何学Go response is blank.

How can i get this data?


By calling getResponseHeader() on the xmlHttpRequest-object, you can get the value of "Location" and see if a redirect is occuring. In that case, you can use the value as the new URL.

$.ajax({
        url: 'http://foo.com/results/bar.aspx?fooid = 123&more=1',
        success: function(data, textStatus, xmlHttpRequest) {
            var newUrl = xmlHttpRequest.getResponseHeader('Location');
            if (newUrl) {
                // Try request again with newUrl as new url...
            }
        }
});

To improve the code, you should first look at the status code to see if a redirect is really happening. The status code should be 301 or 302.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜