Symfony3升级Bootstrap3至4后FOS Comment Bundle报jQuery3兼容错误
Hey there, I’ve run into this exact problem before when upgrading Bootstrap versions, so let’s break down how to fix it step by step.
Why the Error Happens
The TypeError: $.get(...).error is not a function error pops up because jQuery 3 removed the old .error(), .success(), and .complete() methods in favor of Promise-style methods like .fail(), .done(), and .always(). The FOS Comment Bundle 2.0.14 (and earlier versions) still uses the deprecated .error() method in its asynchronous comment JavaScript code, which causes the conflict with jQuery 3.
Solutions to Fix This
1. Modify the Bundle’s JavaScript Directly
This is a straightforward fix if you don’t mind maintaining a small patch:
- Navigate to the FOS Comment Bundle’s JS directory in your project:
vendor/friendsofsymfony/comment-bundle/Resources/public/js - Open the file handling async comments (usually
async.jsorcomment.js) - Replace every instance of
.error(function() { ... })with.fail(function() { ... }) - After making changes, re-publish the bundle’s assets to your web directory:
php bin/console assets:install --symlink - Note: If you update the bundle later via Composer, this change will be overwritten. To avoid this, consider using a tool like
cweagans/composer-patchesto apply and maintain your patch across updates.
2. Use jQuery Migrate Plugin (Quick Temporary Fix)
If you need a fast workaround without modifying bundle code, the jQuery Migrate plugin can restore support for deprecated methods:
- Add the jQuery Migrate script right after your jQuery reference in your base template:
<script src="{{ asset('js/jquery.min.js') }}"></script> <script src="{{ asset('js/jquery-migrate-3.4.0.min.js') }}"></script> <script src="{{ asset('js/bootstrap.min.js') }}"></script> <!-- Other scripts --> - This lets the old
.error()method work with jQuery 3, but it’s not a long-term solution—it adds extra overhead and relies on deprecated functionality.
3. Upgrade the FOS Comment Bundle (Best Long-Term Fix)
Check if a newer version of the bundle supports jQuery 3:
- The 3.x branch of FOS Comment Bundle has been updated to use modern jQuery methods. Upgrade it via Composer:
composer require friendsofsymfony/comment-bundle:^3.0 - Important: Major version upgrades often come with breaking changes, so be sure to review the bundle’s official upgrade documentation first to adjust your project code accordingly.
4. Override the Bundle’s JS with Your Own Version
If you don’t want to modify the vendor directory or upgrade the bundle, you can override the JS:
- Copy the async comment JS file from the bundle into your project’s
web/jsdirectory - Modify the copied file to replace
.error()with.fail() - In your Twig template, replace the bundle’s JS include with your modified version. For example, instead of:
Use:{{ encore_entry_script_tags('fos_comment_js') }} <!-- or the old asset method --> <script src="{{ asset('bundles/foscomment/js/async.js') }}"></script><script src="{{ asset('js/your-modified-async.js') }}"></script>
内容的提问来源于stack exchange,提问作者Fazhom Arnaud

