使用barryvdh / laravel-CORS Laravel angularJS CORS [英] Laravel angularJS CORS using barryvdh/laravel-cors

查看:397
本文介绍了使用barryvdh / laravel-CORS Laravel angularJS CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经6小时,我还没有得到以下问题的解决方案。

It's been six hour and I still don't get the solution for the following problem.

我'试图让AngularJS打我的API从不同的域。在网上搜索后,我发现这的,它说,它可以通过添加CORS(跨域资源共享)头在你的应用程序Laravel支持

I'am trying to get AngularJS hit my API from different domain. After searching the Internet I found this package that it said it can "Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application"

我照着所有的说明。设置此并得到它的工作,但仍然没有运气。我的服务器总是给我同样的以下错误:

I followed all the instructions. Set this and that to get it working but still no luck. My server always send me the same following error :

XMLHtt prequest无法加载<一个href=\"http://lab.laracon/v1/lists?id=123&password=whatever&username=OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3\" rel=\"nofollow\">http://lab.laracon/v1/lists?id=123&password=whatever&username=OSVC8HKKcvCFrsqXsMcbOVwVQvOL0wr3.无访问控制允许来源标头的请求的资源present。原产地 HTTP://lab.angularapi ',因此是不允许访问

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.

这是我的角code:

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配置文件:

Here's my barryvdh laravel-cors config file :

'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服务器配置:

and finally here's my nginx server configuration :

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';

    }

谁能帮我?这有什么错我的code和配置?谢谢

Can anyone help me? what's wrong with my code and configuration ? thanks

推荐答案

最后,我发现我的情况得到妥善解决:

Finally, I found the proper solution for my situation:


  1. 我彻底摆脱barryvdh / laravel-CORS

  2. 感谢丹霍里根为他的鸣叫

简单的CORS与laravel

不过,我改变code点点(我真的不知道为什么$响应 - >包头中>设置();不工作相反,我已将此添加到我的控制器:

However, I change the code little bit (I don't really know why $response->headers->set(); not working. Instead, I added this to my controller :

public function __construct()
    {
        $this->afterFilter(function(){

            header('Access-Control-Allow-Origin: *');

        });
    }

和它像一个老板:)

这篇关于使用barryvdh / laravel-CORS Laravel angularJS CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆