I'm using Symfony 3.1 and I try set a session attribute with AJAX. I saved something in the session before in a controller. Now I created a new controller for the AJAX request that looks like this:
AjaxController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class AjaxController extends Controller {
/**
* @Route("/addId/{id}", name="addId")
*/
public function hideCookieMessageAction(Request $request, $id) {
$session = $request->getSession();
$session->set('id', $id);
$response = new Response();
return $response;
}
}
ajax.js
$(document).ready(function() {
$('.addIdButton').click(function {
var id = 1;
$.ajax({
type: 'POST',
url: 'addId/' + id,
async: false,
})
.fail(function() {
alert('Couldn\'t add flat to wishlist');
});
});
});
I don't get any AJAX or JS error message. But if I look into the Symfony Profiler there is no new Session Attribute. I tried it with a cookie too and it worked. So I think it has nothing to do with a wrong AJAX call but with a wrong session handling in the controller.
Does someone has an idea?