【问题标题】:Axios post request return failedaxios post请求返回失败
【发布时间】:2020-09-19 13:01:26
【问题描述】:

我使用 fetch() 登录它工作正常,但是当我想使用 axios 它返回失败并抛出错误 from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource 。我想用 axios 替换 fetch() 数据,有人可以帮我吗

    (async () => {
  const rawResponse = await fetch(`https://cors-anywhere.herokuapp.com/${config.apiPath}/login`, {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ email: data.email, password: data.password })
  });
  const content = await rawResponse.json();

  console.log(content);
})()

【问题讨论】:

  • 使用axios向我们展示代码
  • 我要转成axios
  • 服务器是否启用了cors
  • 是的............
  • 可能是服务端配置不正确,这个错误与客户端无关。

标签: javascript reactjs axios fetch


【解决方案1】:
axios.post(`https://cors-anywhere.herokuapp.com/${config.apiPath}/login`, {
    headers: {
      'Access-Control-Allow-Origin', '*',
      'Access-Control-Allow-Headers', 'Content-Type, Accept',
      'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH'
    },
    body: JSON.stringify({ email: data.email, password: data.password })
});    

【讨论】:

    猜你喜欢
    • 2018-04-19
    • 2020-08-04
    • 2020-05-31
    • 2020-10-26
    • 1970-01-01
    • 2020-11-26
    • 2012-12-07
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多