V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zficode
V2EX  ›  前端开发

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

  •  
  •   zficode · 2023-01-09 10:45:57 +08:00 · 1188 次点击
    这是一个创建于 444 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    ```
    我想拿到这个 src 对响应标头
    296727
        5
    296727  
       2023-01-09 13:38:33 +08:00
    自己再写一个服务转发
    iPhone11
        6
    iPhone11  
       2023-01-09 15:17:09 +08:00 via iPhone
    // 使用 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
        7
    zficode  
    OP
       2023-01-10 20:58:06 +08:00
    @iPhone11 跨域了怎么办
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   941 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 20:51 · PVG 04:51 · LAX 13:51 · JFK 16:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.