Solve - $.ajax is not a function Error in jQuery

avatar

Borislav Hadzhiev

Tue Oct 19 20212 min read

banner

Photo by Artem Kovalev

Solve - TypeError $.ajax is not a function in jQuery #

The "$.ajax is not a function" error occurs when loading the slim version of jQuery and trying to use the ajax function. The ajax function is excluded from the slim jQuery version. To solve the error load the regular jQuery version on your page.

If you're using a bootstrap html template, you have to remove the script that loads the jQuery slim version at the bottom of the template.

Instead of loading the slim version, load the minified version of jQuery. You can find a link to the CDN by going to the jQuery site.

jQuery regular version

Make sure to add a script that points to the minified version and not the slim one.

The error might also occur if you have multiple different versions of the jQuery library loaded on the same page.

Here is a working example of using the ajax function.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <!-- โœ… load jQuery โœ… --> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous" ></script> </head> <body> <button id="btn">Make request</button> <script src="index.js"></script> </body> </html>

Here's the code for the index.js file.

index.js
$(document).ready(function () { $('#btn').click(function () { $.ajax({ url: 'https://randomuser.me/api/', dataType: 'json', success: function (data) { console.log(data); }, }); }); });

We added an even listener to the button. Every time the button gets clicked it makes a GET request to an API and logs the output to the console

If you load the page and click the button, you will see the API response printed in your console.

Conclusion #

To solve the "$.ajax is not a function" error, add the regular jQuery script to the page and remove the slim version. The slim jQuery version does not include the ajax function.

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee