杨苏杭 杨苏杭 - 1 year ago 30
Ajax Question

ajax post to laravel can't redirect,form submit can redirect

First of all, I am a Chinese student,I will try my best to explain my question,but my English not very good.So,sorry.
In my login web ,I use ajax post data(id,passwd...) to a route.after all confirm,if the data was right,login web direct to another web.but the controller function can not redirect .

return redirect('wx/setdev');
//not work in controller function ,but work in route function


If I submit a form to controller .controller can redirect the web.but ajax can't.

$.post("{{route('cfmlogin')}}",{

_token : "{{csrf_token()}}",
user : $('#num').val(),
passwd : $('#passwd').val()
});


I don not know why.
Thanks

Answer Source

Instead of returning an redirect to the ajax response, you have to return the url and let the ajax callback function redirect to the url.

So you need to change

return redirect('wx/setdev');

to

return url('wx/setdev');

and in your jQuery ajax post, pass an third parameter for the success callback function to redirect

$.post("{{route('cfmlogin')}}", {
    _token : "{{csrf_token()}}",
    user : $('#num').val(),
    passwd : $('#passwd').val()
}, function (data) {
    window.location = data;
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download