拉拉维尔重定向::路由不起作用
Laravel Redirect::route not working
我正在尝试将Redirect::route()
与基于SPA ajax的应用程序一起使用,但由于某种原因它不起作用,我的意思是,页面的位置不会更改为应该重定向的位置。我正在尝试在用户登录和注销时使用它。
路线:
Route::get('/', ['as' => 'root','uses' => 'HomeController@index']);
Route::group(['before' => 'ajax'], function() {
Route::get('/login', ['as' => 'login', 'uses' => 'UserController@login'])->before('guest');
Route::post('/login', ['as' => 'signin', 'uses' => 'UserController@signin']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'UserController@logout'])->before('auth');
});
控制器的方法:
public function signin() {
$user = [
'email' => Input::get('email'),
'password' => Input::get('password')
];
if (Auth::attempt($user)) {
//return Redirect::route('root')->with('flash_notice', 'You are successfully logged in!'); // not redirecting
// I have to use a json response instead and refresh from the js function, but I am losing the notice
return Response::json([
'status' => true,
'notice' => 'You are successfully logged in!'
]);
}
return Response::json([
'status' => false,
'error' => 'Your email or password combination was incorrect.'
]);
}
public function logout() {
Auth::logout();
return Redirect::route('root')->with('flash_notice', 'You are successfully logged out.'); // not redirecting
}
正如我评论的那样,我不能使用Redirect::route()
.相反,在响应成功的 ajax 请求时,我被迫在我的 js 函数中使用 window.location.replace()
:
/* Login */
$(document).on('click', '#login-submit', function(event) {
event.preventDefault();
$.post('/login', $('#login-form').serialize(), function(data) {
if (data['status'] == true) {
window.location.replace('/home/main');
} else {
$('#error').removeClass('hidden');
$('#error').html(data['error']);
}
});
});
这样做不仅对我来说是不可取的,而且我也丢失了通知消息;它们只是在瞬间出现,然后在页面加载后消失。
为什么会这样?如何使重定向::route()' 工作?或者至少,当通知只是瞬间出现并消失并设法显示通知时,我如何避免这种丑陋的效果?
问题是你不能根据来自ajax请求的标头重定向用户。如果你想让服务器在ajax请求中执行验证,那么你的方法就是完全正确的。要在成功登录后保留通知,您可以这样做
设置超时
您的用户将有足够的时间来阅读通知。
setTimeout(function() {
window.location.replace('/home/main');
}, 500);
通知旁边的添加继续按钮
他们将有尽可能多的时间,但它可能不是最好的用户体验设计。
$('#results').html('<div class="alert alert-info">You have been successfully logged in. Click here to continue');
$('#results').click(function() {
window.location.replace('/home/main');
});
重定向用户并通过 GET 请求发送通知
将按摩附加为获取参数,并在脚本启动时检查它。
window.location.replace('/home/main?message="You have been successfully logged in"');
然后
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var message = GetURLParameter('message');
if(message) {
$('#results').html('<div class="alert alert-info">' + message + '</div>');
}
相关文章:
- AngularJS路由没有'不起作用
- 手动拒绝路由承诺;不起作用
- Angularjs:ui路由嵌套状态下的绑定不起作用
- angular js路由不起作用
- Codeigniter :通过 AJAX 调用 php 脚本时路由不起作用
- AngularJS 路由功能不起作用
- 带有组件标签的 VueJS 路由不起作用
- 角度路由;在浏览器中直接访问URL时不起作用,但在单击时起作用
- 多语言应用程序中的Angular JS路由不起作用
- 骨干路由匹配;不起作用
- 主干路由不起作用
- Slick Carousel在应用程序动态路由的基础上不起作用
- AngularJS路由示例不起作用
- Angular2中的路由不起作用
- 角度路由分辨率 - 延迟.拒绝不起作用 - 角度 1 + 打字稿
- 为什么我的 AngularJS 应用程序在添加路由时不起作用
- 路由在 Angular 和 nodejs 应用程序中不起作用
- 路由提供程序否则重定向到不起作用
- 路由不起作用 AngularJS,只是添加一个哈希
- ng 路由不起作用指令