簡體   English   中英

從 XMLHttpRequest 重定向 url

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM