Laravel 4 persistent data - jquery ajax submit

I'm having problems with laravel 4.1 sessions and unexpected behavior.

Depending on how I call the controller method, the session is working or not running my application on the POST route - to add items to the basket, which is the session. For some reason, the session is not being updated. However, if I call the same function with a GET request, the function works as expected.

My routes.phpcontains these two routes:

Route::get('testAdd', array('uses' => 'ProductsController@addToCart'));

Route::post('products/addToCart', array('uses' => 'ProductsController@addToCart'));

Both point to the same method.

Currently this method (for testing):

public function addToCart() {

  Session::put("addcarttest", "add to cart");
  return json_encode(Session::get('addcarttest'));

}

If I call a function with the POST method (with form data), I get the expected result and the contents of the session.

However, if I then check the session (using the profiler), it does not exist. No data was saved.

, GET, , , .

, , , POST , , , - GET sessin, POST, - POST .

- , .

- - , Laravel POST v GET? ?

, POST?

Update:

.

, URL-, , . jquery ajax , .

jquery :

$.ajax({
     url: '/products/addToCart',
     type: 'POST',
     async: false,
 })
     .done(function() {
         console.log("success");
     })
     .fail(function() {
         console.log("error");
     })
     .always(function() {
         console.log("complete");
     });

 return false;

async false - , . ( , true).

, submit ajax submit. - - .

? Jquery submit .

+4
3

!

, laravel 3. ajax, .

return json_encode($response);

. , . :

return Response::json($response); 

!

- submit , ajax .

- , , , - , , -

( )

, : http://forumsarchive.laravel.io/viewtopic.php?id=1304

+10

, XML, JSON.

.

\Session::put('LOGADO.ID_LOJA', $id_loja.time());

\Session::save();

AJAX.

+1

, . , . :

echo json_encode(array('auth' => $auth));

:

return Response::json(array('auth' => $auth));

. javascript :

data = $.parseJSON(data);

, ... . , Laravel Response:: json(), . javascript :)

+1

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


All Articles