简体   繁体   English

Laravel7 CORS:被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否 'Access-Control-Allow-Origin'

[英]Laravel7 CORS : blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

Use case: VueJS/Laravel app has inventory.用例:VueJS/Laravel 应用有库存。 Calling Magento2 using SOAP API to update Qty from VueJS/Laravel.使用 SOAP API 调用 Magento2 以从 VueJS/Laravel 更新数量。

Error as shared below :错误如下共享

Access to XMLHttpRequest at ' http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx ' from origin ' http://192.168.0.x ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Access to XMLHttpRequest at ' http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx ' from origin ' http://192.168.0.x ' has been blocked by CORS 策略:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”header。

Note: I am a beginner in this domain.注意:我是这个领域的初学者。 I might be asking stupid questions.我可能会问一些愚蠢的问题。 Please bear with me and requesting help here.请容忍我并在这里请求帮助。

Tools/Apps used:使用的工具/应用程序:

  1. Laravel Framework 7.9.2 /Running on http://192.168.0.x Laravel 框架 7.9.2 /在http://192.168.0.x上运行
  2. VueJS /Running on http://192.168.0.x VueJS /运行在http://192.168.0.x
  3. Magento2 /Running on http://xx.yyy.abc.123 Magento2 /运行在http://xx.yyy.abc.123
  4. Postman (Tool used to test SOAP API) Postman(用于测试 SOAP API 的工具)

Debugging efforts:调试工作:

  1. URL http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx URL http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx
  2. Tried from postman: It works, Apended token into postmane and got the response.从 postman 尝试:它可以工作,将令牌附加到 postmane 并得到响应。
  3. Making AXIOS call from VueJS/Laravel application to Magento2: (Failed)从 VueJS/Laravel 应用程序对 Magento2 进行 AXIOS 调用:(失败)
Origin: http://192.168.0.x 
Magento2: http://xx.yyy.abc.123
  axios.post("http://xx.yyy.abc.123/rest/V1/integration/admin/token?username=admin&password=xxxxx",
  {

  })
  .then((response) =>
  {
        console.log("response.data",response.data);
        this.apiResponse = response.data;
        //this.getproduct();
  })
  .catch(error =>
  {
       alert('ERROR GETCATEGORY!!!! No Data found');
       console.log(error.response);
  });
  1. Checked for 2 days now about CORS error and found that this is enabled by default in Laravel 7. Not sure why I am seeing this error even after using Laravel 7. There are very few answers or solutions specific to VueJS with Laravel 7. Checked for 2 days now about CORS error and found that this is enabled by default in Laravel 7. Not sure why I am seeing this error even after using Laravel 7. There are very few answers or solutions specific to VueJS with Laravel 7.

Question:问题:

  1. What does this error means.这个错误是什么意思。 I thought it is just calling from webpage1 to webpage2, it does not seem to work.我以为它只是从webpage1调用webpage2,它似乎不起作用。
  2. Do I need to make any changes on my VueJS/Laravel application.我需要对我的 VueJS/Laravel 应用程序进行任何更改吗? Am I missing anything further.我还想念什么吗?

Please do let me know if you need any more information to help me in this regards.如果您在这方面需要更多信息来帮助我,请告诉我。 Thanks for your help.谢谢你的帮助。 Regards,问候,

I had got the same CORS error while working on a Vue.js project.在处理 Vue.js 项目时,我遇到了相同的 CORS 错误。 I finally solved this issue just today in the morning.我终于在今天早上解决了这个问题。 You can resolve this either by building a proxy server or another way would be to disable the security settings of your browser (eg, CHROME) for accessing cross origin apis.您可以通过构建代理服务器或其他方式来解决此问题,即禁用浏览器的安全设置(例如,CHROME)以访问跨源 API。 Both these solutions had worked for me.这两种解决方案都对我有用。 The later solution is the easiest solutoion and does not require any mock server or a proxy server to be build.后一种解决方案是最简单的解决方案,不需要构建任何模拟服务器或代理服务器。 Both these solutions can be resolved at the front end.这两种解决方案都可以在前端解决。

You can disable your browser (CHROME) security settings for accessing apis out of the origin by typing the below command on the terminal:您可以通过在终端上键入以下命令来禁用浏览器 (CHROME) 安全设置以从源访问 api:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir="/tmp/chrome_dev_session" --disable-web-security /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir="/tmp/chrome_dev_session" --disable-web-security

After running the above command on your terminal, a new chrome window with security settings disabled will open up.在终端上运行上述命令后,将打开一个禁用安全设置的新 chrome window。 Now, run your program (npm run serve / npm run dev) again and this time you will not get any CORS error and would be able to GET request using axios.现在,再次运行您的程序(npm run serve / npm run dev),这次您将不会收到任何 CORS 错误,并且能够使用 axios 获取请求。

Hope this helps!希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel CORS - 对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin” header 出现在请求中 - Laravel CORS -Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested 角度7中的cors(cors策略:对预检请求的响应未通过访问控制检查)错误 - cors(cors policy: Response to preflight request doesn't pass access control check) error in angular 7 响应预检请求未通过访问控制检查:否'Access-Control-Allow-Origin' - esponse to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' 已被 CORS 政策阻止:无“访问控制允许来源” - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' 如何解决这个'http://localhost:8080'已被CORS策略阻止:响应预检请求没有通过vueJS中的访问控制? - How to solve this 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control in vueJS? header("访问控制允许来源:"); 不工作 CORS 策略,预检请求不允许重定向 - header("Access-Control-Allow-Origin: "); dont work CORS policy, Redirect is not allowed for a preflight request Cors header “访问控制允许来源”被阻止 Api 响应 - Cors header “Access-control-allow-origin” blocked Api Response domain.com 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态 - domain.com has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status CORS 错误:对预检请求的响应 'Access-Control-Allow-Origin' header 包含多个值 '*、*'、 - CORS error: Response to preflight request The 'Access-Control-Allow-Origin' header contains multiple values '*, *', Laravel Cors Access-Control-Allow-Origin - Laravel Cors Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM