Laravel access-control-allow-origin有些方法工作,有些方法没有

时间:2016-05-17 04:11:05

标签: laravel cors

网络客户端使用angularjs访问laravel web api,我使用谷歌搜索建议的中间件,代码如下: 中间件:

public function handle($request, Closure $next)
    {

        return $next($request)
            ->header('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN'])
            // Depending of your application you can't use '*'
            // Some security CORS concerns 
            //->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'POST, OPTIONS')
            ->header('Access-Control-Allow-Credentials', 'true')
            ->header('Access-Control-Max-Age', '10000')
            ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
    }

路线:

Route::group(['prefix' => 'stock','middleware' =>'authapi'],function(){

    Route::post('trendvote',function(){return _index::trendVote(Input::all());});//this one report cors errors

    Route::post('trendindexminute',function(){return _index::getIndexMinute(Input::all());});//this one still works fine

});

它工作正常直到今天它突然一个方法报告再次出现错误h,奇怪的是:其他方法也使用相同的midddleware正常工作,请帮助,提前谢谢

1 个答案:

答案 0 :(得分:0)

这有点遗憾,问题是内部500错误可以让cors发生,所以解决方案实际上是修复了bug,然后事情正常工作XD