怎样获取请求发往服务器的真实地址

使用: xhr.responseURL; 

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/test', true);
xhr.onload = function () {
  // 返回 http://example.com/test
  console.log(xhr.responseURL);
};
xhr.send(null);

 

注意:

1. xhr.responseURL和xhr.open()中的URL可能是不一样的, 因为服务器也可能发生跳转, 前者返回的是最后实际返回数据的URL;

2. 如果原始请求URL包含锚点(#), 则该属性会将锚点抽离;

posted on 2019-09-20 12:12  aisowe  阅读(682)  评论(0编辑  收藏  举报

导航