前端获取短链访问后的链接

2023-01-09 10:45:57 +08:00
 zficode

现在我有一个短链: https://sourl.cn/6wKy5d ,它指向 baidu, 然后我需要前端去调用这个地址,获取它重定向的地址,这种前端有可能实现吗

1219 次点击
所在节点    前端开发
7 条回复
opengps
2023-01-09 10:47:23 +08:00
用隐藏的 ifream 去访问短连接,过一会在检查下网址变化
296727
2023-01-09 11:10:01 +08:00
一个 xhr 请求之后就可以回去 res 了
zficode
2023-01-09 11:14:38 +08:00
@296727 跨域的话能获取响应头吗
zficode
2023-01-09 11:21:31 +08:00
现在我的想法是:
```
let img = document.createElement('img')
img.src = 'https://sourl.cn/rC6BuB'

```
我想拿到这个 src 对响应标头
296727
2023-01-09 13:38:33 +08:00
自己再写一个服务转发
iPhone11
2023-01-09 15:17:09 +08:00
// 使用 XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://sourl.cn/6wKy5d', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
// 获取重定向地址
var responseURL = xhr.responseURL;
console.log(responseURL);
}
}
xhr.send();

// 使用 fetch
fetch('https://sourl.cn/6wKy5d', {
redirect: 'follow'
}).then(res => {
// 获取重定向地址
var responseURL = res.url;
console.log(responseURL);
});



哪里不明白,我再补充
zficode
2023-01-10 20:58:06 +08:00
@iPhone11 跨域了怎么办

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/907517

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX