Laravel 5.7 - Ajax call and Session::get()

Asked

Viewed 211 times

0

Some days I’m banging my head to read a dice Session through a ajax call.

In an application without Framework I do this normally using the PHPSESSION, however in Laravel 5.7 I can’t do it at all.

My environment: Ubuntu + PHP 7.2 + Laravel 5.7 server using file as Session driver

Man ajax call:

$.ajax({
    url: '/nota/consulta?id_cliente='+id_cliente+'&item='+item,
    dataType: 'json',
    success: function(result) {
        console.log(result);
    }
});    

My controller:

public function getNota(Request $request) 
{
    if ($request->session()->has('nota')) 
    {
        $nota = $request->session()->get('nota');
        return response()->json($nota);
    }   
    return response()->json(array('msg'=>'erro'));
}

The session exists because I print it on View, but the result of ajax does not carry the data. I believe that there is something in the Laravel which is preventing me from reading Session data when the request is made by Ajax, because if I open the link in the browser the data appears.

  • 1

    What is your route?

  • Hi Virgilio, I am using Routes web.php with the following route: Route::get('/note/query', 'Trocacontroller@getnote');

  • console.log returns what?

1 answer

-1

Pass data to route using post, no parameters are passed in ajax url

$.ajax({
    url: '/nota/consulta,
    dataType: 'json',
    method: 'post',
    data: {
      id_cliente: id_cliente,
      item : item
    }
    success: function(result) {
        console.log(result);
    }
});  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.