本地主机上的访问控制允许来源(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
Access-Control-Allow-Origin (Laravel & Vue) CORS CORB error on localhost
提问by Ahmed Mamdouh
I use this package and it didn't work (https://github.com/barryvdh/laravel-cors)
我使用这个包但它没有用(https://github.com/barryvdh/laravel-cors)
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 .htaccess
or 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 R
key from keyboard. Then type cmd
in box and enter).
在 Windows 中运行 CMD(同时按下 Windows 按钮和R
键盘上的键。然后cmd
在框中输入并输入)。
Secondly go to C
drive (in CLI) or where your chrome installed (using cd
command) 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 CORS
or CORB
error.
它将以禁用的网络安全模式打开您的浏览器。完成后,您现在可以使用这种CORS
或CORB
错误进行测试。
note: before running this command make sure there are no chrome are opened already.
注意:在运行此命令之前,请确保没有打开任何 chrome。
Program Files (x86)\Google\Chrome\Application\chrome.exe
this 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 CORS
or CORB
error
回答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