[英]get redirected url from XMLHttpRequest
I'd like to get the redirect url from a site via XMLHttpRequest我想通过 XMLHttpRequest 从站点获取重定向 url
Here's the code below这是下面的代码
let url = 'https://some-site.com/Z/12345/54321'
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(response) {
var xhr2 = response.target;
console.log(xhr.location)
if (xhr.readyState === 4 && xhr.status === 302) {
var redirectURL = xhr.location;
}
}
xhr.onerror = (response) => {
debugger
}
xhr.open('GET', url, true);
xhr.send();
so the problem is, the website, as the code below所以问题是,网站,如下代码
let url = 'https://some-site.com/Z/12345/54321'
is only redirecting to another website只是重定向到另一个网站
I only want to get the redirect url我只想获得重定向 url
I don't know how the 'https://some-site.com/Z/12345/54321'
coded我不知道'https://some-site.com/Z/12345/54321'
是如何编码的
so the TLDR is send request to 'https://some-site.com/Z/12345/54321'
=> from 'https://some-site.com/Z/12345/54321'
redirected to the url that I want to get
所以 TLDR 发送请求到'https://some-site.com/Z/12345/54321'
=> 从'https://some-site.com/Z/12345/54321'
重定向到the url that I want to get
Is there any way to get the url?有什么办法可以得到 url?
You should use XMLHttpRequestInstance.responseURL :您应该使用XMLHttpRequestInstance.responseURL :
const xhr = new XMLHttpRequest;
xhr.open('GET', 'https://some-site.com/Z/12345/54321'); xhr.responseType = 'json'
xhr.onload = function(){ /* no need to use onreadystatechange in xhr2 */
// this.response is JSON response
// this.responseURL is what you want
}
xhr.onerror = function(e){
// e is ErrorEvent Obj
}
xhr.send();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.