不允许 Laravel 中的“Access-Control-Allow-Origin”标头

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

Allow No 'Access-Control-Allow-Origin' header in Laravel

phpangularjslaravelnginx

提问by user3569641

how can I allow laravel No 'Access-Control-Allow-Origin' header is present on the requested? Because when I try to $http.get()a certain url using angular.js I always get error about it.

我怎样才能允许 laravel No 'Access-Control-Allow-Origin' header is present on the requested?因为当我尝试$http.get()使用 angular.js 访问某个 url 时,我总是会出错。

回答by user3569641

Oh, got it. I just need to add before filter on my application.

哦,明白了。我只需要在我的应用程序上添加过滤器。

filters.php

过滤器.php

App::before(function($request)
{
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Requested-With');
    header('Access-Control-Allow-Credentials: true');
});