Axios发布请求返回失败

时间:2020-09-19 13:01:26

标签: javascript reactjs axios fetch

我正在使用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);
})()

1 个答案:

答案 0 :(得分:-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 })
});