angularjs - Laravel angularJS CORS 使用 barryvdh/laravel-cors

标签 angularjs nginx laravel cors

已经六个小时了,我仍然没有得到以下问题的解决方案。

我试图让 AngularJS 从不同的域访问我的 API。在网上搜索后,我找到了这个 package它说它可以“在您的 Laravel 应用程序中添加 CORS(跨源资源共享) header 支持”

我遵循了所有说明。设置这个和那个让它工作,但仍然没有运气。我的服务器总是向我发送相同的以下错误:

XMLHttpRequest cannot load http://lab.laracon/v1/lists?id=123&password=whatever&username=OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://lab.angularapi' is therefore not allowed access.



这是我的 Angular 代码:
var Demo = angular.module( "Demo", [ "ngResource" ] );
Demo.controller(
  "ListController"
  function( $scope, ,$http, $resource ) {

    $http.defaults.useXDomain = true;

     $scope.useResource = function() {
     var Lists = $resource('http://lab.laracon/v1/lists', {
         username: 'OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3',
         password: 'whatever'
     });
     Lists.get({
         id: 1
     }, function(data) {
         alert(data.ok);
     });
   };

  }
);

这是我的 barryvdh laravel-cors 配置文件:
'defaults' => array(
        'allow_credentials' => false,
        'allow_origin' => array(),
        'allow_headers' => array(),
        'allow_methods' => array(),
        'expose_headers' => array(),
        'max_age' => 0,
    ),

    'paths' => array(
        '^/v1/' => array(
            'allow_origin' => array('*'),
            // 'allow_headers' => array('Content-Type'),
            'allow_headers' => array('*'),
            'allow_methods' => array('POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'),
            'max_age' => 3600,
        ),
    ),

最后这是我的 nginx 服务器配置:
location / {

        # URLs to attempt, including pretty ones.
        try_files   $uri $uri/ /index.php?$query_string;

        add_header 'Access-Control-Allow-Origin' 'http://lab.angularapi';
         add_header 'Access-Control-Allow-Credentials' 'false';
         add_header 'Access-Control-Allow-Headers' '*';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';

    }

谁能帮我?我的代码和配置有什么问题?谢谢

最佳答案

最后,我找到了适合我的情况的正确解决方案:

  • 我完全摆脱了 barryvdh/laravel-cors
  • 感谢 Dan Horrigan 的推文

  • Simple CORS with laravel

    但是,我稍微更改了代码(我真的不知道为什么 $response->headers->set(); 不起作用。相反,我将其添加到我的 Controller 中:
    public function __construct()
        {
            $this->afterFilter(function(){
    
                header('Access-Control-Allow-Origin: *');
    
            });
        }
    

    它像老板一样工作:)

    关于angularjs - Laravel angularJS CORS 使用 barryvdh/laravel-cors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23135127/

    相关文章:

    node.js - 跨 node.js 和 nginx 的身份验证

    php - mysql从表中选择所有但只更改一个列名

    javascript - 如何使用 Angular Js 检查复选框值以及存储在数据库中的现有逗号分隔值

    sql-server - 如何在 Angular 中将同步调用变成异步调用?

    ajax - 如何在 AppHarbor 上接受 CORS AJAX 请求?

    nginx - 使用 php-fpm + nginx 时无法获取错误堆栈跟踪或错误日志

    php - Laravel 模型函数获取相关实体描述为字符串

    laravel - 无法在 Laravel 4 中运行原始查询

    javascript - 如何使用 Angular 正确路由多个 html 页面

    javascript - 如何按类名选择元素并遍历angularJs中的那些对象?