[英]get redirected url from XMLHttpRequest
我想通过 XMLHttpRequest 从站点获取重定向 url
这是下面的代码
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();
所以问题是,网站,如下代码
let url = 'https://some-site.com/Z/12345/54321'
只是重定向到另一个网站
我只想获得重定向 url
我不知道'https://some-site.com/Z/12345/54321'
是如何编码的
所以 TLDR 发送请求到'https://some-site.com/Z/12345/54321'
=> 从'https://some-site.com/Z/12345/54321'
重定向到the url that I want to get
有什么办法可以得到 url?
您应该使用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.