Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax request not working for laravel 5.0

I am doing Google Oauth login using Google Api's in Laravel 5.0. I get the data of currently logged in user's email,id_token and now I want to send these data to the controller(SigninController) for calling our own api and get the response back to the front end (signin.blade.php) via an Ajax query . But My Ajax query is not working. I am attaching the codes here .

My Signin.blade.php file's ajax looks like(I have included 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 routes.php looks like :

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

In my SigninController's signinProcessOauth function normal "Request for Form" is working but "Request->ajax()" maybe not working . It looks like :

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

Everytime I run the code, I get "error!! ajax failure !!!!" response i.e. ajax's failure function is called . I can't figure it out Where the problem is? Or Is there any other way to to send the datam from view to controller and get back the response to the frontend ?

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

like image 501
Risul Islam Avatar asked Nov 09 '22 23:11

Risul Islam


1 Answers

Change your url as follow:

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

Hope this will work.

like image 189
Rumel Avatar answered Nov 14 '22 21:11

Rumel