views:

49

answers:

1

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 response is blank.

How can i get this data?

A: 

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.

edwin
Have you tried this? It does not work in Chrome, and based on the w3c's XmlHttpRequest spec, I don't think it would work in other browsers. The browser is supposed to handle redirects either by transparently redirecting (therefore no redirect header) or, if the redirect URL breaks same origin policy, returns an error (still no 30x header). If you know otherwise, please let me know,as it is very crucial for my project.
Anthony