Ajax Laravel 5.1 API在用户注册后将所有请求重定向到/home
我有史以来最奇怪的问题 目前,我在我的Laravel5.1项目中有2条路由,我将使用它们作为我的apiAjax Laravel 5.1 API在用户注册后将所有请求重定向到/home,ajax,api,redirect,laravel-5,response,Ajax,Api,Redirect,Laravel 5,Response,我有史以来最奇怪的问题 目前,我在我的Laravel5.1项目中有2条路由,我将使用它们作为我的api Route::post('register', 'Auth\AuthController@postRegister'); Route::post('login', 'Auth\AuthController@postLogin'); 第一次从前端到“注册”路径进行ajax调用时,一切都按预期进行 但是,如果我对任何laravel路由进行另一个ajax调用,它会将我重定向到“主”路由 我的代码:
Route::post('register', 'Auth\AuthController@postRegister');
Route::post('login', 'Auth\AuthController@postLogin');
第一次从前端到“注册”路径进行ajax调用时,一切都按预期进行
但是,如果我对任何laravel路由进行另一个ajax调用,它会将我重定向到“主”路由
我的代码:
授权控制器:
public function postRegister(CreateUserRequest $request)
{
User::createUser($request);
return response()->json();
}
public function postLogin(Request $request)
{
if (!Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
return response()->json()->setStatusCode(403);
}
return response()->json();
}
型号:
public static function createUser($data)
{
DB::table('users')->insert([
'f_name' => $data->f_name,
'l_name' => $data->l_name,
'gender' => $data->gender,
'email' => $data->email,
'birth_date' => $data->birth_date,
'password' => bcrypt($data->password),
'activation_code' => $data->activation_code
]);
}
Ajax调用:
$.ajax({
url: Api.route('register'),
method: 'POST',
data: requestData,
statusCode: {
200: function()
{
console.log('register: 200');
},
422: function()
{
console.log('register: 422');
}
}
});
我试过:
- 进行第一次ajax调用,然后注释掉控制器中的所有内容,只返回一个字符串,但在第二次请求时,它仍然将我重定向到“主”路由
- 如果我注释掉“DB::insert(…”,它的工作原理与预期的一样
- 如果我使“DB::insert()”没有任何数据,它也会像预期的那样工作
- 如果我使用任何单个字段生成“DB::insert()”,它会在第二次调用时重定向
- 如果我使用默认请求而不是自定义请求,它仍在重定向
非常感谢您的任何建议 应用于AuthController的所有路由的
guest
中间件,除了getLogout
因此,对这些路由的任何ajax调用都会重定向到home
路由(检查RedirectIfAuthenticated
中间件),但getLogout
除外
因此,您应该注销用户,以便按照预期工作
设置以下路线并调用它:
Route::get('logout', 'Auth\AuthController@postLogout');
或手动注销用户:
Auth::logout();
您是否在使用任何
中间件
?我在AuthController中使用laravels默认中间件public function\uu construct(){$this->middleware('guest',['except'=>'getLogout'])}
@pespantelisw当你说任何laravel路由时,你是指AuthController的任何路由吗?是的,我还没有任何其他控制器。