Skip to content

http重定向

  • 服务器

    javascript
    // 原理:状态码设置为 302 / 301 时,浏览器会去响应头中找 Location,对该地址发起
    // 新的请求
    
    // express
    res.redirect("some url")
    
    // node
    res.statusCode = 302
    res.setHeader('Location', '/url')
    // 或者
    res.writeHead(302, { Location: '/url' })
  • 客户端

    javascript
    window.location.href="Some URL";  
    window.location.replace("Some URL");
    html
    // 5s后刷新页面
    <meta http-equiv="refresh" content="5"> 
    
    // 5s后跳转到 127.0.0.1:5500/index.html
    <meta http-equiv="refresh" content="5;url=index.html">