2

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?

2
  • seems all correct, try to refresh the page before check for the session attribute Commented Sep 21, 2016 at 9:26
  • Reload the page to see new session attribute. Commented Sep 21, 2016 at 20:25

1 Answer 1

1

Check out your browser's debug tool.

I assume you are using Chrome in Windows, Ctrl+Shift+i opens Chrome's debug tool, click Network tab, and then do your AJAX request (click the buttn $('.addIdButton')), then the debug tool will show a new record, check the record if you request is really ok (status is 200 means ok otherwise it failed)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.