我尝试在jquery中使用ajax设置为laravel添加用于get / post请求的自定义标头。但是似乎没有用。
我的预期结果是将每个带有自定义标头的请求添加到laravel(以通过中间件进行验证),并且仅在成功时返回视图。
Ajax代码:
$.ajaxSetup({
headers: {
'X-Token': 'xxxxxx'
}
});
中间件:
public function handle($request, Closure $next, $guard = null)
{
$token = $request->header('X-Token');
if(!empty($token)){
return $next($request); //token received and continue
}
else{
return ''; // no token received and stop here
}
}
视图:
public function index(){
return View::make("dashboard")->render(); //if middleware pass, return view
}
网络路线:
Route::group(['middleware' => 'token.verify'], function()
{
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'HomeController@index']);
});
视图是整个视图(不是局部视图)。