Vue和Axios CORS错误请求的资源上没有"Access-Control-Allow-Origin"标头

Jam*_*ons 12 javascript get cors vue.js axios

我目前收到上述错误,我正在使用Axios向外部API发出GET请求.在阅读了Mozilla文档后,做了大量的研究并尝试了不同的选择,我仍然没有任何好转.

我已经将代码剥离回基础:

axios.get('URL.com', {
        headers: {
          Access-Control-Allow-Origin: *
        },
        auth: {
          username: 'username',
          password: 'password'
        },
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
Run Code Online (Sandbox Code Playgroud)

我是否需要在标题中添加其他内容?

一切都通过Postman工作,所以一旦我能通过CORS问题,一切都会奏效.

Leo*_*ipe 1

baseURL: 'https://www.yourserver.com.br',
timeout: 10000,
withCredentials: false
Run Code Online (Sandbox Code Playgroud)

设置 axios.defaults.withCredentials = true;

  • 问候。这里的正确答案需要更多的解释而不仅仅是代码。你能多谈谈你的意图吗? (8认同)
  • 虽然此代码片段可能是解决方案,但[包括解释](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers)确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而那些人可能不知道您建议代码的原因。 (3认同)