How to add Ajax capabilites to Symfony

后端 未结 4 715
旧巷少年郎
旧巷少年郎 2021-01-20 03:47

I have a set of sessions in a page, which I want to remove using AJAX. i.e click on a link, and without having to navigate for a new page, just remove the session, and show

4条回答
  •  耶瑟儿~
    2021-01-20 04:37

    In your AJAX call you use global event object, which is not cross-browser and shouldn't be used (e.g. Firefox doesn't have it).

    Pass event in function call instead:

    $('#remove_session').click(function(e){
        e.preventDefault();
    
        // rest of your code
    });
    

    If you do it that way, jQuery takes care about cross-browser normalization and proper behaviour.

    See also this question to learn more about difference between global event object and event passed into function.


    If it still doesn't work

    • open developer tools in Chrome (F12)
    • check in your console (Console tab) if there are any error reports after you click a button
    • also, open Network tab and see if after clicking a button any request is made; what is response status?
    • make sure your controller works with GET requests (as you don't specify request type, and GET is default)
    • you may also try adding dataType: "json" to your AJAX request, but this shouldn't be a problem as Symfony's JsonResponse should already provide neccessary response headers

    Check other places

    • error you posted says where the unexpected : is; investigate that line; is the code you have posted here the whole javascript that you have on your page?

提交回复
热议问题