Ajax 和状态 302
我遇到了 ajax(和/或 jQuery ajax)和状态 302 的问题。
这就是我正在尝试的:
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);
some_page_that_I_cant_change.php
重定向到 .bin
302
代码。我不想下载这个文件,我只想知道该文件的路径。示例:
./some_page_that_I_cant_change.php
./path/to/bin/file.bin << I wan't only this path as string
问题是 Chrome 自动重定向到该文件,而不告诉我脚本的路径。有解决方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 HEAD 请求而不是 GET 请求,我怀疑这会达到您想要的效果。您应该只在响应中看到标头,没有响应正文(即不会下载 .bin)。
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).
有一个关于完全相同主题的旧线程,其中包含解决方案和解决方法:
如何在 jQuery Ajax 之后管理重定向请求致电
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
一句话:我不认为你能做到这一点,因为 HTTP 协议。
HTTP 协议([1]:http://www.w3.org/Protocols/ rfc2616/rfc2616-sec10.html)规定302表示重定向URI,浏览器遵循。所以,javascript无法获得重定向响应,只能获得最终响应。
所以,你必须通过响应体获取路径。
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.