Javascript Laravel重定向::路由不工作

Javascript Laravel重定向::路由不工作,javascript,jquery,ajax,laravel,laravel-4,Javascript,Jquery,Ajax,Laravel,Laravel 4,我试图在基于SPA ajax的应用程序中使用Redirect::route(),但由于某些原因,它无法正常工作,我的意思是,页面的位置不会更改为应该重定向的位置。我尝试在用户登录和注销时使用此选项 路线: Route::get('/', ['as' => 'root','uses' => 'HomeController@index']); Route::group(['before' => 'ajax'], function() { Route::get('/logi

我试图在基于SPA ajax的应用程序中使用
Redirect::route()
,但由于某些原因,它无法正常工作,我的意思是,页面的位置不会更改为应该重定向的位置。我尝试在用户登录和注销时使用此选项

路线:

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');
});
然后

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>');
}
函数GetURLParameter(sParam) { var sPageURL=window.location.search.substring(1); var sURLVariables=sPageURL.split('&'); 对于(变量i=0;i您能再解释一下最后一个选项吗?我不完全明白。以后如何检索该值?我已更新了答案。有关检索查询参数的详细信息,请参阅。
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>');
}