Using Ajax in Laravel 4.2, the login form does not redirect to the intended page once the user is authenticated
我正在使用Laravel4.2中的jquery验证插件,并希望通过Ajax提交一个登录表单。一切正常,但是当表单被验证并且用户被验证时,表单不会重定向到预期的页面。
实际上,用户是经过身份验证的(因为当我刷新时,页面会转到用户的配置文件),但是提交表单时,
这是我的jquery代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | var validator = $('#login-form').validate({ rules: { email: { required: true }, password: { required: true } }, submitHandler: function(form) { $.ajax({ url: 'login', type: 'post', cache: false, data: $(form).serialize(), dataType: 'json', success:function(data){ $('.message').text(data.error); $('.message').fadeIn(200); } }); $(form).ajaxSubmit(); } }); |
这是我的控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class AccountController extends BaseController { public function logInForm() { return View::make('pages.login'); } public function logIn() { $validator = Validator::make(Input::all(), array( 'email' => 'required|email', 'password' => 'required' ) ); if ($validator->fails()) { return Response::json([ 'error' => $validator->errors()->first() ]); } else { $auth = Auth::attempt(array( 'email' => Input::get('email'), 'password' => Input::get('password') )); if ($auth) { return Redirect::intended('/'); } else { return Response::json([ 'error' => 'Email or password is wrong.' ]); } } return Response::json([ 'error' => 'Sorry, your request could not be proceeded.' ]); } } |
您最好在响应中返回预期的位置,然后让客户机执行重定向。
1 2 3 | if ($auth) { return Response::json(['redirectUrl' => 'http://domain.com/dash']); } |
在客户中
1 | window.location = response.redirectUrl; |