我正在研究一个AJAX脚本,该脚本向缩短的URL发出GET请求,就像bit.ly链接一样.我如何实际获取最终/重定向页面的URL?
我可以从重定向页面获取响应标头,但它似乎不包含URL信息:
$.ajax({
url: "http://www.thislinkredirects.com",
success: function(data, status, xhr) {
var response = $(data);
},
complete: function(xhr) {
console.log(xhr.getAllResponseHeaders());
}
});
Run Code Online (Sandbox Code Playgroud)
结果:
Date: Tue, 23 Jun 2015 03:22:07 GMT
Allow: POST, GET, OPTIONS, PUT, DELETE
Server: lighttpd/1.4.35
X-Powered-By: PHP/5.3.3
Transfer-Encoding: chunked
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Content-type: text/html
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: accept, authorization
Run Code Online (Sandbox Code Playgroud)
我无法控制发出get请求的服务器.
小智 9
当我在这里回答类似的问题时:https : //stackoverflow.com/a/48445360/1398908
更好的解决方案是为jQuery的ajax提供一个自定义xhr对象,如下所示:
var xhr = new XMLHttpRequest();
$.ajax({
url: '/url',
type: 'post',
data: '...',
xhr: function() {
return xhr;
}
});
Run Code Online (Sandbox Code Playgroud)
然后您可以在任何回调中访问当前URL
success: function () {
console.log(xhr.responseURL);
}
Run Code Online (Sandbox Code Playgroud)
注意:IE不支持responseURL属性。(请参阅文档)
| 归档时间: |
|
| 查看次数: |
6544 次 |
| 最近记录: |