Ajax calls stopped working in my Symfony 2.8 application

Update: One day after I wrote this post, version 2.8.36 was released to fix this problem. It seem that other versions of Symfony were also affected. If you are interested in this problem, read on. If you are affected by it, you should upgrade, not use the fix from here.

Last night I implemented a few improvements in the Metadiscourse annotator which I am developing in my spare time. (I rewrote several functions that cut the running time of two actions from minutes to seconds … but that’s for a different post, maybe …). I tested the new code on my development computer and everything seemed perfect, so I deployed the new version. Just before sending an email to the people who use this program, I noticed that a particular functionality that relied on AJAX requests no longer worked. This was completely unrelated to the changes I made. Initially, I thought it is because I restricted the HTTP method to GET, whilst the request was done using POST. I fixed that, but it was still not working. After spending a bit of time, I realised that none of the AJAX requests worked anymore. This was at 1am, how else 🙂

After imagining all kind of possible causes for this, I finally realised that the problem was only in the dev environment and that the deployed system worked fine. The problem seems to be caused by changes in the WebProfilerBundle that were integrated in the version 2.8.35 of Symfony. To know whether it affects you have a look in the javascript console of your favourite browser. If you get an error

Uncaught TypeError: Cannot read property 'count' of undefined

and see something like this, then you probably are.

The fix is quite easy and is already available on github. Hopefully it will be integrated in the next release of Symfony. If you cannot wait till then, you will need to modify the renderAjaxRequests function in

vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base_js.html.twig

from

to

The lines in yellow highlight the differences between the two functions. After you make the change everything should be fine. I don’t know whether this bug affects the 3.x and 4.x branches of Symfony, but it’s worth checking.

 

 

Leave a Reply