ajax请求不适用于laravel 5.0


ajax request not working for laravel 5.0

我使用Laravel 5.0中的Google Api登录Google Oauth。我获得了当前登录用户的电子邮件id_token的数据,现在我想将这些数据发送到控制器(SigninController),用于调用我们自己的api,并通过Ajax查询将响应返回到前端(signin.blade.php)。但是My Ajax查询不起作用。我把代码附在这里。

我的Signin.blad.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函数中,正常的"请求表单"正在工作,但"请求->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";
    }

每次运行代码时,我都会得到"error!!ajax failure!!!"响应,即调用ajax的failure函数。我搞不清楚问题出在哪里?或者有没有其他方法可以将数据从视图发送到控制器,并将响应返回到前端?

感谢您耐心阅读这么长的帖子。:):)

更改您的url如下:

url: '{!! route('post_signin_oauth') !!}'

希望这能奏效。

在方法中,$request不是数组,而是对象。因此,您需要使用->来访问属性。