Ajax request not working for laravel 5.0

I am using Google Oauth using Google Api in Laravel 5.0. I get data about the currently registered user email, id_token, and now I want to send this data to the controller (SigninController) to call our own api and get a response back to the front end (signin.blade.php) via an Ajax request. But my Ajax request is not working. I am attaching codes here.

My Signin.blade.php ajax file looks like (I included the csrf header):

$.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 !!!!"); } }); 

My .php routes look like this:

  Route::post('/signin/oauth', [ 'uses' => ' SigninController@signinProcessOauth ', 'as' => 'post_signin_oauth', ]); 

In my SigninController SigninProcessOauth function, the normal "Request for form" works, but "Request-> ajax ()" may not work. It looks like this:

  . . 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"; } 

Every time I run the code, I get "error! Ajax failure !!!!" answer i.e. ajax failure function called. I can not understand where the problem is? Or is there another way to send data from the view to the controller and return the response to the external interface?

Thank you for reading such a long post patiently. :) :)

+5
source share
2 answers

In your method, $request not an array, its object. Therefore, you need to use -> to access properties.

0
source

Change your URL as follows:

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

Hope this works.

0
source

Source: https://habr.com/ru/post/1238142/


All Articles