【问题标题】:How to request anonymously via Axios?如何通过 Axios 匿名请求?
【发布时间】:2020-02-07 10:54:01
【问题描述】:

我想通过 axios 向一个 url 发送一个 GET 请求。

网站似乎可以通过 cookie 识别我,并将 url 重定向到登录用户移动到的页面。

我想发送一个请求,就好像我是从一个隐身标签页发送请求一样

我尝试过使用withCredentials: false 配置。

在我的情况下,这似乎对 truefalse 没有任何影响。

 axios({
        url : url,
        method: 'get',
        maxRedirects: 15,
        headers: {
          'Content-Type': 'text/html',
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods':'GET',
        },
        withCredentials: false, // without cookies so no login
      })
      .then((res) => {
         console.log("Response", res.data)
      })
      .catch((err) => {
         console.log("Error with fetch: ", err)
      })
    });

【问题讨论】:

  • 您尝试使用哪个网址?
  • @SaurabhMistry 它是本地的。但想象一下,它类似于 Facebook 主页。如果用户已登录,则显示其他内容,如果未登录,则显示其他内容。 (在我的情况下,它也重定向到用户页面)

标签: javascript reactjs cookies request axios


【解决方案1】:

我无法通过 axios 实现这一点。但是找到了一种使用 fetch api 的方法

在没有以前的 cookie 或登录的情况下,我得到了完美的输出


fetch(url, { 
        credentials: 'omit',
      }).then((response)=>{
        return response.text();
      }).then((data)=>{
        console.log(data);
      }).catch(err=>{
        console.log(err);
      })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-10
    • 2019-11-06
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2018-07-14
    相关资源
    最近更新 更多