Javascript Vue 和 Axios CORS 错误请求的资源上不存在“Access-Control-Allow-Origin”标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41778860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:37:54  来源:igfitidea点击:

Vue and Axios CORS error No 'Access-Control-Allow-Origin' header is present on the requested resource

javascriptgetcorsvue.jsaxios

提问by James Parsons

I am currently getting the above error, I am using Axios to make the GET request to an external API. After reading the Mozilladocs, doing a lot of research and trying different options I am still not any better off.

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

I have stripped the code back to the basics:

我已将代码剥离回基础:

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);
      });

Do I need to add anything else to the headers?

我需要在标题中添加任何其他内容吗?

Everything works through Postman so once I can pass the CORS issue everything will work.

一切都通过 Postman 工作,所以一旦我可以通过 CORS 问题,一切都会正常。

回答by Leonardo Filipe

baseURL: 'https://www.yourserver.com.br',
timeout: 10000,
withCredentials: false

setting axios.defaults.withCredentials = true;

设置 axios.defaults.withCredentials = true;