3

I'm having a problem with ajax (and/or jQuery ajax) and status 302.

This is what I am trying:

var xhr = new XMLHttpRequest();
xhr.open("GET", 'some_page_that_I_cant_change.php');
xhr.onreadystatechange = function(){
    console.log(xhr.readyState, xhr.status);
};
xhr.send(null);

The some_page_that_I_cant_change.php redirects to a .bin with 302 code. I do not want to download this file, I just want know the path to the file. Example:

./some_page_that_I_cant_change.php
./path/to/bin/file.bin << I wan't only this path as string

The problem is that Chrome automatically redirects to the file, without telling me the path to script. Is there a workaround for this?

helloandre
  • 10,541
  • 8
  • 47
  • 64
David Rodrigues
  • 12,041
  • 16
  • 62
  • 90

3 Answers3

2

If you use a HEAD request instead of a GET request, I suspect that will do what you want. You should only see headers in the response, no response body (i.e., no .bin will download).

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • Thanks. But it don't works on Chrome. It continues to download the file. But, I tried to change to "`async:false`" and have a surprise: the file don't is downloaded, instead, throw a exception `"NETWORK_ERR: XMLHttpRequest Exception 101"`. I can't reproduce on jsFiddle, but the header that I want, I can't get too, only simple headers ([code here](http://jsfiddle.net/RL22m/)) – David Rodrigues Jun 09 '11 at 21:12
  • That's silly. Chrome shouldn't download a resource with a HEAD call :-( I haven't got any other ideas honestly, sorry. – Platinum Azure Jun 09 '11 at 21:27
  • No problems. Thanks anyway. I up too because can help someone with similar problems. – David Rodrigues Jun 10 '11 at 00:32
0

In a word: I don't think you can do that cause of HTTP protocol.
HTTP protocol([1]:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) stipulate 302 means redirect URI, and browsers follows.So,the javascript can't get the redirect response, just get final response.

So,You have to get path by response body.

Chanble
  • 11
  • 2
0

There's an older thread about exactly the same topic with solutions and work arounds: How to manage a redirect request after a jQuery Ajax call

Community
  • 1
  • 1
Adriaan Tijsseling
  • 2,025
  • 1
  • 18
  • 23