本地主机上的访问控制允许来源(Laravel 和 Vue)CORS CORB 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54545554/
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-09-14 18:16:31  来源:igfitidea点击:

Access-Control-Allow-Origin (Laravel & Vue) CORS CORB error on localhost

laravelapivue.jscors

提问by Ahmed Mamdouh

I use this package and it didn't work (https://github.com/barryvdh/laravel-cors)

我使用这个包但它没有用(https://github.com/barryvdh/laravel-cors

enter image description here

在此处输入图片说明

addSMS() {
    axios.post('https://smsmisr.com/api/webapi', {
                headers: {
                        'Access-Control-Allow-Origin': '*',
                        'Access-Control-Allow-Methods': 'POST, GET, PUT, OPTIONS, DELETE',
                        'Access-Control-Allow-Headers': 'Access-Control-Allow-Methods, Access-Control-Allow-Origin, Origin, Accept, Content-Type',
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    },
                username: '*****',
                passowrd: '****',
                etc: '',
            })
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });
        }

please tell me how to fix it it's cors not working for me.

请告诉我如何解决它的 cors 对我不起作用。

回答by Rejoanul Alam

As you have no access in API's source code so there is nothing to do with .htaccessor adding 'Access-Control-Allow-Origin': '*',in script. But still you can eradicate this error by opening chrome browser with disabled security mode

由于您无法访问 API 的源代码,因此与脚本无关.htaccess或添加'Access-Control-Allow-Origin': '*',脚本。但是您仍然可以通过打开禁用安全模式的 chrome 浏览器来消除此错误

In windows run CMD (press together windows button and Rkey from keyboard. Then type cmdin box and enter).

在 Windows 中运行 CMD(同时按下 Windows 按钮和R键盘上的键。然后cmd在框中输入并输入)。

Secondly go to Cdrive (in CLI) or where your chrome installed (using cdcommand) then run following command

其次转到C驱动器(在 CLI 中)或安装 chrome 的位置(使用cd命令),然后运行以下命令

“Program Files (x86)\Google\Chrome\Application\chrome.exe” –-allow-file-access-from-files --disable-web-security --user-data-dir --disable-features=CrossSiteDocumentBlockingIfIsolating

It will open your browser in disabled web security mode. Once you done it you can test now with this kind of CORSor CORBerror.

它将以禁用的网络安全模式打开您的浏览器。完成后,您现在可以使用这种CORSCORB错误进行测试。

note: before running this command make sure there are no chrome are opened already.

注意:在运行此命令之前,请确保没有打开任何 chrome。

Program Files (x86)\Google\Chrome\Application\chrome.exethis should be your chrome installed path

Program Files (x86)\Google\Chrome\Application\chrome.exe这应该是你的 chrome 安装路径

However after opening browser it will show as below, dont panic & dont close this message. if you close this message it will again through this CORSor CORBerror enter image description here

但是,打开浏览器后,它将显示如下,不要惊慌,不要关闭此消息。如果您关闭此消息,它将再次通过此CORSCORB错误 在此处输入图片说明

回答by Kamrul Touhid

Update config/cors.php

更新配置/cors.php

   'supportsCredentials' => false,
   'allowedOrigins' => ['*'],
   'allowedHeaders' => ['*'],
   'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT',  'DELETE']
   'exposedHeaders' => ['*'],
   'maxAge' => 0,

   then run
   php artisan config:cache 
   command i think it will help you