我在Laravel 5.0中使用Google Api进行Google Oauth登录.我得到当前登录用户的电子邮件的数据,id_token,现在我想将这些数据发送到控制器(SigninController)以调用我们自己的api并通过Ajax将响应返回到前端(signin.blade.php)查询.但我的Ajax查询不起作用.我在这里附上代码.
我的Signin.blade.php文件的ajax看起来像(我已经包含了csrf头文件):
$.ajax({ url: '/signin/oauth', type:"POST", data: data, headers: { 'X-CSRF-Token' : token}, success:function(data){ console.log(data); if(data){ console.log("Success nowwww for ajax expected data!"); // window.location.href = '{{url("/home")}}'; } else{ console.log("Success ajax ! But not expected data!"); // window.location.href = '{{url("/signup")}}'; } },error:function(){ alert("error!! ajax failure !!!!"); } });
我的routes.php看起来像:
Route::post('/signin/oauth', [ 'uses' => 'SigninController@signinProcessOauth', 'as' => 'post_signin_oauth', ]);
在我的SigninController的signinProcessOauth函数中,正常的"表单请求"正在运行但"Request-> ajax()"可能无法正常工作.看起来像 :
. . use Illuminate\Http\Request; use Illuminate\Support\Facades\Session; . . public function signinProcessOauth(Request $request) { $getData = $request->ajax(); if ($getData) { $authCode = $request['authCode']; $idToken = $request['idToken']; $userEmail = $request['userEmail']; // call the api here and send the above data to the server and process the response like saving the cookie etc return $authCode; // return according to the response,this will return in ajax success function,right now it is authcode just for testing purpose } return "error"; }
每次运行代码时,我都会收到"错误!! ajax失败!!!!" 响应即调用ajax的失败函数.我无法弄明白问题出在哪里?或者有没有其他方法将数据从视图发送到控制器并返回对前端的响应?
谢谢你耐心地阅读这么长的帖子.:) :)