在iframe中调用ajax时,我遇到了laravel的路由问题,但我真的不明白是什么原因造成的,因为我所拥有的应该调用ajax并成功返回。任何帮助将不胜感激。我尝试使用返回响应::json( $response );,以及一大堆不同的东西,但无济于事。我仍然有这个 500 错误。我也运行了作曲家转储自动加载。任何关于为什么失败的帮助和解释将不胜感激。
这是我的 ajax 代码
$.ajax({
type: "POST",
url: baseLocalUrl, //baseLocalUrl= "http://localhost:4567/admin/menuBuilder/1/save"
data:
{
html: $("#menuHTML").text()
},
success: function(data){
alert("success!");
}
});
这是我的路线
Route::group(array('prefix' => 'admin', 'before' => 'auth'), function()
{
.....
Route::post('menuBuilder/{role}/save' , array('uses' => 'AdminMenuBuilderController@saveHTML' ));
.....
});
这是我的控制器方法
public function saveHTML($roleId){
return Response::json(array('status' => 'OK'));
}
这是我收到的错误
POST http://localhost:4567/admin/menuBuilder/1/save 500 (Internal Server Error)
拉拉维尔日志:
production.ERROR: 500 - Exception @ /admin/menuBuilder/1/save
exception 'IlluminateSessionTokenMismatchException' in /vagrant/app/filters.php:98
过滤器.php
<?php
/*
|--------------------------------------------------------------------------
| Application & Route Filters
|--------------------------------------------------------------------------
|
| Below you will find the "before" and "after" events for the application
| which may be used to do any work before or after a request into your
| application. Here you may also register your custom route filters.
|
*/
App::before(function($request)
{
//
});
App::after(function($request, $response)
{
//
});
/*
|--------------------------------------------------------------------------
| Authentication Filters
|--------------------------------------------------------------------------
|
| The following filters are used to verify that the user of the current
| session is logged into this application. The "basic" filter easily
| integrates HTTP Basic authentication for quick, simple checking.
|
*/
Route::filter('auth', function()
{
if ( Auth::guest() ) // If the user is not logged in
{
return Redirect::guest('user/login');
}
});
Route::filter('auth.basic', function()
{
return Auth::basic();
});
/*
|--------------------------------------------------------------------------
| Guest Filter
|--------------------------------------------------------------------------
|
| The "guest" filter is the counterpart of the authentication filters as
| it simply checks that the current user is not logged in. A redirect
| response will be issued if they are, which you may freely change.
|
*/
Route::filter('guest', function()
{
if (Auth::check()) return Redirect::to('user/login/');
});
/*
|--------------------------------------------------------------------------
| Role Permissions
|--------------------------------------------------------------------------
|
| Access filters based on roles.
|
*/
// Check for role on all admin routes
Entrust::routeNeedsRole( 'admin*', array('admin'), Redirect::to('/') );
// Check for permissions on admin actions
Entrust::routeNeedsPermission( 'admin/blogs*', 'manage_blogs', Redirect::to('/admin') );
Entrust::routeNeedsPermission( 'admin/comments*', 'manage_comments', Redirect::to('/admin') );
Entrust::routeNeedsPermission( 'admin/users*', 'manage_users', Redirect::to('/admin') );
Entrust::routeNeedsPermission( 'admin/roles*', 'manage_roles', Redirect::to('/admin') );
/*
|--------------------------------------------------------------------------
| CSRF Protection Filter
|--------------------------------------------------------------------------
|
| The CSRF filter is responsible for protecting your application against
| cross-site request forgery attacks. If this special token in a user
| session does not match the one given in this request, we'll bail.
|
*/
Route::filter('csrf', function()
{
if (Session::getToken() != Input::get('csrf_token') && Session::getToken() != Input::get('_token'))
{
throw new IlluminateSessionTokenMismatchException;
}
});
/*
|--------------------------------------------------------------------------
| Language
|--------------------------------------------------------------------------
|
| Detect the browser language.
|
*/
Route::filter('detectLang', function($route, $request, $lang = 'auto')
{
if($lang != "auto" && in_array($lang , Config::get('app.available_language')))
{
Config::set('app.locale', $lang);
}else{
$browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
$browser_lang = substr($browser_lang, 0,2);
$userLang = (in_array($browser_lang, Config::get('app.available_language'))) ? $browser_lang : Config::get('app.locale');
Config::set('app.locale', $userLang);
App::setLocale($userLang);
}
});
看看你的csrf过滤器,它等待你的输入有csrf_token
或_token
带有令牌的密钥。
我建议在执行ajax
请求时将其作为标头提供:
Route::filter('csrf', function()
{
$token = Request::ajax() ? Request::header('x-csrf-token') : (Input::get('csrf_token') ?: Input::get('_token'));
if (Session::token() != $token)
{
throw new IlluminateSessionTokenMismatchException;
}
});
和客户端:
$.ajax({
type: 'post',
url: url,
datatype: 'json',
beforeSend: function(request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='token']").attr('content'));
},
})