【发布时间】:2016-02-04 17:28:42
【问题描述】:
我正在按照本教程使用 Laravel 和 Angular 使用令牌设置身份验证。
https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps
这很好用,但是当我单独托管 Laravel(作为后端),并将 Angular 前端托管在另一个域上时,我在控制台中得到了愚蠢的错误:-
XMLHttpRequest cannot load http://jotdot.mysite.com/api/authenticate.
Response to preflight request doesn't pass access control check: The
'Access-Control-Allow-Origin' header contains multiple values '*, *', but
only one is allowed. Origin 'http://jotdotfrontend.mysite.com' is
therefore not allowed access.
我在 Laravel 中放置了一个 CORS 中间件,它适用于简单的路由。
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request)->header('Access-Control-Allow-Origin', '*')- >header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
}
}
如何向其中添加 CORS 中间件:-
Route::group(['prefix' => 'api', 'middleware' => 'cors'], function()
{
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController@authenticate');
});
在 ['prefix' => 'api'] 旁边添加它并没有解决问题。
谢谢
【问题讨论】:
-
您能否更新路由示例以显示您如何将中间件添加到路由组定义中?
-
@watcher - 我更新了它,我还有另一种方法,但它仍然没有工作
标签: angularjs laravel authentication laravel-5 cors