简体   繁体   English

使用Laravel CORS的Angularjs'Access-Control-Allow-Origin'

[英]Angularjs 'Access-Control-Allow-Origin' using Laravel CORS

I have just installed Laravel CORS by barryvdh , the module works fine to my knowledge but i seem to be still facing the Access-Control-Allow-Origin error 我刚刚安装了barryvdh的Laravel CORS,该模块在我所知的范围内可以正常工作,但是我似乎仍然会遇到 Access-Control-Allow-Origin错误

XMLHttpRequest cannot load http://acns.example.com:8000/status/d6uIlvwwi8PrvQe4kQfufyZ0LlqQqJyGeyJjdC …I4OTYzMTJlYzYyMmMxOTVkNWI5YjNjYzM1MTczNyIsInMiOiI2ZDcwZDg5N2FkOTQxZDFkIn0=. XMLHttpRequest无法加载http://acns.example.com:8000/status/d6uIlvwwi8PrvQe4kQfufyZ0LlqQqJyGeyJjdC…I4OTYzMTJlYzYyMmMxOTVkNWI5YjNjYzM1MTczNyIsInMiOiI2Z5K No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://www.example.com:8000 ' is therefore not allowed access. 因此,不允许访问来源“ http://www.example.com:8000 ”。

below is my angular code to execute the function: 下面是我执行该功能的角度代码:

var SomeApp = angular.module('SomeApp',['ngResource','ngSanitize'])
SomeApp.factory('SomeAppService', ['$resource', function($resource){

    return {          
       firstActions : $resource(svrid('acns') + '/action/:payload',{payload:'@payload'},
                {
                    'remove': {method:'DELETE',isArray:true, cache:false},
                    'save' : {method:'POST', isArray:true, cache:false},
                    'query' : {method:'GET', isArray:true,cache:false},
                }, {cache:false}        
        ),

        //some more other functions
    };

 }]);

While further diving into the code, i realize that the supposingly appended headers are not being included in the xhr request (refer image below) 在进一步研究代码时,我意识到xhr请求中未包含假定附加的标头(请参见下图)

错误

What am I missing here? 我在这里想念什么?

Update 1: I slightly narrow down the problem most probably related to barryvdh's laravel-cors that uses asm89's stack-cors where by the config\\cors.php is not properly passed to asm89. 更新1:我稍微缩小了问题,最有可能与使用asm89的stack-cors的 barryvdh的laravel-cors有关 ,其中config \\ cors.php未正确传递给asm89。 Not very confident with the problem but i did some manual override which causes OPTIONS to work when i manually pass the array in config\\cors.php to asm89 but then on the other hand causes other methods to fail. 对这个问题不是很自信,但是当我手动将config\\cors.php的数组传递给asm89时,我进行了一些手动覆盖,这导致OPTIONS起作用,但另一方面,导致其他方法失败。

Update 2: i tried manually alter a section under Asm89\\Stack\\CorsService give it a default value like such: 更新2:我尝试手动更改Asm89\\Stack\\CorsService下的部分, Asm89\\Stack\\CorsService提供默认值,例如:

private function normalizeOptions(array $options = array())
    {
            $options += array(
            'allowedOrigins' => array('*'),
            'supportsCredentials' => true,
            'allowedHeaders' => array('*'),
            'exposedHeaders' => array('*'),
            'allowedMethods' => array('*'),
            'maxAge' => 0,
        );
        // Some other codes
        // normalize array('*') to true

        return $options;
    }

and comment out one small section 并注释掉一小部分

public function handlePreflightRequest(Request $request)
{
    /*if (true !== $check = $this->checkPreflightRequestConditions($request)) {
    return $check;
    }*/

    return $this->buildPreflightCheckResponse($request);
}

It works perfectly for preflight OPTIONS and GET method but POST and DELETE method will prompt me with an error 它非常适合预检OPTIONSGET方法,但POSTDELETE方法将提示我错误

No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://www.example.com:8000 ' is therefore not allowed access. 因此,不允许访问来源“ http://www.example.com:8000 ”。 The response had HTTP status code 500. 响应的HTTP状态码为500。

After the preflight OPTIONS request 预检OPTIONS请求后

您需要在服务器端添加CORS响应标头。

I was having a similar issue, where all cross-domain AJAX requests would work, expect for POST requests. 我遇到了一个类似的问题,所有跨域AJAX请求都可以正常工作,但需要POST请求。 The POST responses would be missing the 'Access-Control-Allow-Origin' header, but GET and DELETE requests functioned as expected. POST响应将缺少“ Access-Control-Allow-Origin”标头,但是GET和DELETE请求的功能正常。

It turned out that I had disabled suppression of deprecated warning in php.ini, and by using 原来,我已经通过使用禁用了php.ini中不推荐使用的警告的抑制

$data = Input::json()->all();

in my controller, PHP was throwing a 在我的控制器中,PHP抛出了一个

 PHP Deprecated:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

warning, followed by a 警告,然后是

PHP Warning:  Cannot modify header information - headers already sent

warning. 警告。

This meant that my custom headers were never sent. 这意味着我的自定义标头从未发送过。 I fixed the problem by simply suppressing the deprecated warnings in php.ini. 我通过简单地抑制php.ini中不推荐使用的警告来解决此问题。

The deprecated problem I will have to deal with in the future, but for now it's a functioning workaround to getting POST data from JSON requests in laravel. 将来我将不得不处理不建议使用的问题,但是目前,这是一种从laravel中的JSON请求获取POST数据的可行方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM