laravel 5.2 登录界面 ajax异步请求登录接口,在接口中设置Session,跳转后取不值
登录界面通过 ajax调用接口登录
登录ajax:
$.ajax({
dataType:'json',
type: "POST",
url: "/api/login",
data: {username:username,password:password,check_code:check_code},
success: function(data){
var ecode = data.code;
// alert(ecode);
if(ecode == 0){
location.href="/";
return;
}else{
alert(data.msg);//
return;
}
},
error: function () {
alert("系统错误,请联系系统运维");//
}
});
中间件:
class VerifyIsLogin
{
public function handle($request, Closure $next, $guard = null)
{
if(Session::has('user'))
{
return redirect('/login');
}
return $next($request);
}
}
login api:
public function api_login(Request $request)
{
Session::put('user','hello world');
return response()->json(['code' =>0, 'msg' => '']);
}
结果不能正常跳转到主页,也就是session中的user没有值,加Session::save()也没用,
如果换成$_SESSION当然是可以的
登录ajax:
$.ajax({
dataType:'json',
type: "POST",
url: "/api/login",
data: {username:username,password:password,check_code:check_code},
success: function(data){
var ecode = data.code;
// alert(ecode);
if(ecode == 0){
location.href="/";
return;
}else{
alert(data.msg);//
return;
}
},
error: function () {
alert("系统错误,请联系系统运维");//
}
});
中间件:
class VerifyIsLogin
{
public function handle($request, Closure $next, $guard = null)
{
if(Session::has('user'))
{
return redirect('/login');
}
return $next($request);
}
}
login api:
public function api_login(Request $request)
{
Session::put('user','hello world');
return response()->json(['code' =>0, 'msg' => '']);
}
结果不能正常跳转到主页,也就是session中的user没有值,加Session::save()也没用,
如果换成$_SESSION当然是可以的
4 个回复
zhangyouchao
赞同来自:
meditatorzhang
赞同来自:
RoughRoar
赞同来自:
微米粒_小程
赞同来自: