我正在按照本教程設置使用Laravel和Angular的令牌認證。認證中的Laravel角度CORS問題
https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps
這工作得很好,但是當我主持Laravel(作爲後端)分開,並在另一個域角前端,我得到的foolowing錯誤控制檯: -
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', '[email protected]');
});
添加它旁邊[ '前綴'=> 'API']沒有解決的問題。
感謝
可以更新路由例子來告訴你如何添加的中間件路由組定義? –
@watcher - 我更新了它,還有另一種方式,但它仍然無效 –