I’m attempting to create an AJAX request. Here’s my function definition:
function AJAXrequest(url, postedData, callback) {
$.ajax({
type: 'POST',
url: url,
data: postedData,
dataType: 'json',
success: callback
});
}
Here’s where I call it, providing the parameters:
AJAXrequest('voting.ajax.php', data, function(data) {
// function body
});
Yet the callback does not run, and instead I get a console.error
:
TypeError: $.ajax(...) is not a function.
Why?
asked Aug 16, 2013 at 10:31
marked-downmarked-down
9,81822 gold badges87 silver badges150 bronze badges
10
Neither of the answers here helped me. The problem was: I was using the slim build of jQuery, which had some things removed, ajax being one of them.
The solution: Just download the regular (compressed or not) version of jQuery here and include it in your project.
answered Nov 21, 2016 at 3:46
GusGus
15.3k7 gold badges25 silver badges28 bronze badges
13
Double-check if you’re using full-version of jquery and not some slim version.
I was using the jquery cdn-script link that comes with jquery. The problem is this one by default is slim.jquery.js which doesn’t have the ajax
function in it. So, if you’re using (copy-pasted from Bootstrap website) slim version jquery script link, use the full version instead.
That is to say use
<script src="https://code.jquery.com/jquery-3.1.1.min.js">
instead of
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"
answered Sep 12, 2017 at 20:28
karthikskarthiks
7,0497 gold badges47 silver badges62 bronze badges
1
Not sure, but it looks like you have a syntax error in your code. Try:
$.ajax({
type: 'POST',
url: url,
data: postedData,
dataType: 'json',
success: callback
});
You had extra brackets next to $.ajax
which were not needed. If you still get the error, then the jQuery script file is not loaded.
user664833
18.2k19 gold badges91 silver badges139 bronze badges
answered Aug 16, 2013 at 10:32
Jason EvansJason Evans
28.9k14 gold badges88 silver badges154 bronze badges
7
Reference the jquery min version that includes ajax:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
answered Aug 30, 2019 at 18:15
I used following version of jQuery and it worked
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
answered Mar 17, 2021 at 12:05
ASANIANASANIAN
3722 silver badges8 bronze badges
Don’t use the slim build of jQuery, which doesn’t have the ajax function in it. Use the minified version instead from https://releases.jquery.com/
Note: Make sure to not use the jquery link copied from the Bootstrap website, because they use the slim version.
answered Aug 29, 2020 at 18:20
Zafar FaheemZafar Faheem
2,0061 gold badge5 silver badges7 bronze badges
I know this is an old posting — and Gus basically answered it.
But in my experience, JQuery has since changed their code for importing from the CDN — so I thought I would go ahead and post their latest code for importing from the CDN:
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
answered Oct 14, 2020 at 23:24
b0rgBart3b0rgBart3
3041 silver badge5 bronze badges
There is a syntax error as you have placed parenthesis after ajax function and another set of parenthesis to define the argument list:-
As you have written:-
$.ajax() ({
type: 'POST',
url: url,
data: postedData,
dataType: 'json',
success: callback
});
The parenthesis around ajax has to be removed it should be:-
$.ajax({
type: 'POST',
url: url,
data: postedData,
dataType: 'json',
success: callback
});
answered Aug 16, 2013 at 10:34
abhinsitabhinsit
3,2044 gold badges20 silver badges26 bronze badges
If you are using bootstrap html template remember to remove the link to jquery slim at the bottom of the template.
answered Oct 6, 2019 at 9:35
Lily H.Lily H.
1641 silver badge10 bronze badges
0
You have an error in your AJAX function, too much brackets, try instead $.ajax({
answered Aug 16, 2013 at 10:34
netvision73netvision73
4,8311 gold badge23 silver badges30 bronze badges
I encountered the same question, and my solution was: add the JQuery script.
Especially, we should make sure the corresponding JQuery is loaded when we debug our js under the firefox/chrome.
answered May 4, 2016 at 6:19
Jianeng XuJianeng Xu
1071 silver badge9 bronze badges
0
For anyone trying to run this in nodejs: It won’t work out of the box, since jquery needs a browser (or similar)! I was just trying to get the import to run and was logging console.log($)
which wrote [Function]
and then also console.log($.ajax)
which returned undefined
. I had no tsc
errors and had autocomplete from intellij, so I was wondering what’s going on.
Then at some point I realised that node
might be the problem and not typescript. I tried the same code in the browser and it worked. To make it work you need to run:
require("jsdom").env("", function(err, window) {
if (err) {
console.error(err);
return;
}
var $ = require("jquery")(window);
});
(credits: https://stackoverflow.com/a/4129032/3022127)
answered Jun 20, 2017 at 14:26
berslingbersling
17.5k8 gold badges60 silver badges74 bronze badges
I included bootstrap scripts and the first one(jquery.slim) is the reason of this error. Removing the first row solved my problem.
answered Sep 14, 2022 at 12:19
MrAlbinoMrAlbino
3082 silver badges10 bronze badges
-
It may be because of the slim build of JQuery. Try to use ‘uncompressed’ or ‘minified’ builds. (JQuery CDNs)
-
It also may be because of loading Bootstrap CDN before the JQuery CDN. In my case, I removed the Bootstrap CDN and move the JQuery one to the end of the ‘body’ tag and the problem was solved.
answered Dec 13, 2022 at 12:24
let me share my experience:
my html page designer use:
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
when I create a simple AJAX request, getting an error it says,
TypeError: $.ajax(…) is not a function
So, i add:
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
then, it perfectly works for me at least.
answered May 11, 2020 at 19:16
1
The Ajax is not a function is an Ajax request error. It occurs due to various reasons, such as: Using the wrong Ajax version and syntax errors.
However, there are other reasons behind this error, and this article covers all of them, including its various solutions. Let’s begin.
Contents
- Why Is the Ajax Is Not a Function Error Message Occurring?
- – The Version Doesn’t Support Ajax
- – Syntax Error
- – Use JQXHR as a Function
- – Ajax Functions Are Missing
- How To Resolve the “Ajax Is Not a Function” Error Message?
- – Correct the Syntax Error
- – Run the Program in a Browser
- – Use the Full Version Instead of the Slim Ajax Version
- – Add a Script Correctly
- FAQs
- 1. What Is the Difference Between Jquery Ajax and Ajax?
- 2. What Is AjaxCDN?
- Conclusion
Why Is the Ajax Is Not a Function Error Message Occurring?
The reason why the Ajax is not a function error message occurs is that the programmer is using the wrong version of Ajax. However, other reasons, such as syntax errors or compatibility issues, also play a role in the occurrence of this error.
Some other reasons are listed below.
- The version doesn’t support Ajax.
- Syntax error
- Use jqXHR as a function.
- Ajax functions are missing.
– The Version Doesn’t Support Ajax
The major reason why this error message occurs is that the version of the function files the programmer is using does not support Ajax. Many web pages send AJAX requests to a server.
Because the response relies on the cooperation of the server and the network between the client and the server, the user can expect AJAX errors, such as: Your JavaScript program receives an error response instead of data. This occurs because the program has to wait too long for the response.
Therefore, when the function and webpage are incompatible with each other, the response will occur, due to which the error message arises.
– Syntax Error
In such types of error messages, syntax errors are the biggest reason behind them. Programmers use wrong functions or operations, due to which errors occur. Some common Ajax error messages are listed below.
- “uncaught typeerror: $.ajax is not a function jquery slim.
- $.ajax is not a function laravel.
- $.ajax is not a function node js.
- $.ajax(…).done is not a function.
- $.ajax is not a function bootstrap 4.
- $.ajax is not a function chrome console “.
– Use JQXHR as a Function
When the programmer uses jQXHR as a function instead of an operation, the error occurs. Do remember that jQXHR is not a function.
When an operand is used as a function, the compiler cannot execute it properly and thus throw the programmer an error message in return. Let’s see the example below.
For example:
$.ajax({
type: ‘POST’,
URL: URL,
data: posted-Data,
dataType: ‘.json’,
success: call-back
});
}
Explanation:
Upon the execution of the above program, the output will contain an error message. This is because the user has called $.ajax without arguments “$.ajax()” due to which the return value is a jQXHR object. JQXHR can never be an object. Thus, $.ajax() will throw an error message.
– Ajax Functions Are Missing
When the programmer is using the slim version of Ajax, which is set as default, the functions will be missing, and the program will not work smoothly.
Therefore, not only will the program won’t work, but it will also be full of structural errors, including various syntax errors. So, the programmer should confirm from their browser’s settings whether they’re using the slim version or not.
How To Resolve the “Ajax Is Not a Function” Error Message?
In order to resolve the Ajax is not a function error message, the programmer has to check and correct the program’s syntax errors and use the correct version of Ajax. However, other reasons include using the correct json file or trying to run the program on the browser.
– Correct the Syntax Error
The first thing to check in order to remove this error message is by checking the program’s syntaxes and identifying its errors. If they’re found, the programmer can use the manual method to correct them. Otherwise, there are special software just for this purpose, so they can use them.
In the below example, the programmer has made a syntax error by placing parentheses after the Ajax function and then adding another set of parentheses in order to define the argument list. See the example below/
Wrong example:
type: ‘POST’,
url: url,
data: posted-Data,
dataType: ‘json’,
success: call-back
});
Correct example:
The programmer has to remove the parentheses around the ajax function. The program should look like this:
type: ‘POST’,
url: url,
data: posted-Data,
dataType: ‘json’,
success: call-back
});
– Run the Program in a Browser
The programmer has to run the program in the browser after correcting the error in the node.js file. For programmers who try to run the program in nodejs, it will show an error. This is because jQuery needs a browser to work.
Some programmers might try to get the import to run the program and may be logging console.log($) with incompatible functions, and when trying to console.log($.ajax), it returns undefined; thus, causing an error.
Therefore, this means that the problem lies in “node ” and not in typescript. So, try to run the same program in the browser, and the error will be removed. Look at the example below.
For example:
if (err) {
console.error(err);
return;
}
var $ = required(“jquery”)(window);
});
– Use the Full Version Instead of the Slim Ajax Version
The programmer should use the full version of Ajax or jquery and not the slim version. This is because the full version contains all the functions and helps the program run smoothly. Moreover, using the full version of Ajax will remove the error message as well.
If the programmer is using the jquery cdn-script link that comes with jquery, it is by default set to slim.jquery.js, which doesn’t have the ajax function in it. The details of both versions are given below.
Slim version syntax:
src=”http://code.jquery.com/jquery-3.4.1.slim.min.js”
integrity=”sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8=”
crossorigin=”anonymous”></script>
Regular version syntax:
<script
src=”https://code.jquery.com/jquery-3.4.1.min.js”
integrity=”sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=”
crossorigin=”anonymous”></script>
– Add a Script Correctly
Ensure that any scripts the programmer includes redirect to the minified version rather than the slim one. By doing so, the error will be eliminated. Having numerous instances of the jQuery library loaded on the same page may also result in the issue. Here is an example of the ajax function in operation.
For example:
<html lang=”eg”>
<head>
<meta-charset=”UTF-9″ />
< load jQuery >
<script
src=”https://code.jquery.com/jquery-3.4.1.min.js”
integrity=”sha256-CSXorXvZcTkaix6Yvo6Hk7tPXikynS7ogEvDej/m4=”
cross-origin=”anonymously”
></script>
</head>
<body>
<button-id=”btn”>Make your request</button>
<script src=”index.js”></script>
</body>
</html>
Code for the index.js file:
$(‘#btn’)click.(function () {
$.ajax({
url: ‘http://random-users.me/api./’,
dataType: ‘.json’,
success: function.(data) {
console-log(data);
},
});
});
});
FAQs
1. What Is the Difference Between Jquery Ajax and Ajax?
The difference between jquery ajax and Ajax is that jQuery is a JavaScript library for designing and making web development tasks easy for programmers. It lets the programmers run javascript outside of the browser. Whereas, AJAX is a web development method for making asynchronous calls to the server.
2. What Is AjaxCDN?
AJAX CDN is the Content Delivery Network (CDN) that hosts popular third-party JavaScript libraries, i.e., jQuery. It also enables the user to easily add them to their web applications. The user can use jQuery by adding a <script> tag to their page that points to ajax.aspnetcdn.com.
Conclusion
After reading this guide, the reader can understand the logic behind this error and how they can resolve it using different methods. A few points to notice are:
- Never use the slim version of ajax, as it does not contain all the functions.
- The programmer should be careful while typing functions because they can use the function as an operand and vice versa.
- Run the JQuery program on the browser instead of in nodejs to eliminate the error.
The reader can easily resolve similar ajax errors on their own by reading this guide. Thank you for reading!
- Author
- Recent Posts
Your Go-To Resource for Learn & Build: CSS,JavaScript,HTML,PHP,C++ and MYSQL. Meet The Team
AJAX( Asynchronous JavaScript And XML) is a set of web development techniques that uses many web technologies on the client-side to create asynchronous web applications. With Ajax, web applications can asynchronously send and retrieve data from a server without interfering with the existing page’s display or behavior.
AJAX is not a programming language. It requires a built-in browser with the XMLHttpRequest object that requests data from the server, and it uses JavaScript and HTML DOM to display data.
Problem
Let’a create a simple AJAX request that returns some data from a MySQL database:
function AJAXrequest(url, postedData, callback) {
$.ajax() ({
type: 'POST',
url: url,
data: postedData,
dataType: 'json',
success: callback
});
}
You call the AJAX request,
AJAXrequest('newajax.php', Data, function(data) {
});
but you get the following error on your console:
TypeError: $.ajax(...) is not a function.
Solution
The solution is pretty simple. Many programmers make the mistake of using the jquery cdn-script link that comes with jquery. By default, this is a slim version of jquery, (slim.jquery.js
), not the full jquery version. This slim version lacks some key modules, especially the ajax function. Therefore, the slim version does not recognize the ajax function.
To use the ajax function, use:
<script src="https://code.jquery.com/jquery-3.1.1.min.js">
Do NOT use:
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"
Overview
In this article, we’ll cover a common stumbling block when working with JQuery.
TypeError: $.ajax is not a function
You may encounter an error similar to the following:
Uncaught TypeError: $.ajax is not a function at HTMLDocument.<anonymous> ((index):86) at j (jquery-3.4.1.slim.min.js:2) at k (jquery-3.4.1.slim.min.js:2)
The issue here is that the Slim version of JQuery doesn’t support Ajax.
Slim Version
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8=" crossorigin="anonymous"></script>
Regular Version
Instead, use the regular version of JQuery and the error will go away:
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
The TypeError: $.ajax is not a function occurs when you use a slim version of jQuery that does not include the ajax function. To solve this error, you will have to use a regular version of jQuery instead of using the slim version.
If you face any error that indicates that it is not a function, that means the function you are trying to call is not defined in your code. That’s why you keep getting this error. We have given the instruction with the example, so you can follow them to fix this error:
- First of all, go to the jQuery website and copy the <script> tag of the minified jQuery version or take it from the below.
<script src="https://appuals.com/wp-content/litespeed/localres/aHR0cHM6Ly9jb2RlLmpxdWVyeS5jb20vjquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
- Now replace it with your jQuery script tag.
Changing the src value of the jQuery script tag - After that, If you have already working on a project getting this error, then skip these steps and try to check if the error is fixed.
- Create a button with a named Send request and then create a script tag with the src of index.js.
Creating a button and Script tag - Make a file with the name index.js and paste the following code to make a request.
$(document).ready(function () { $('#btn').click(function () { $.ajax({ url: 'https://api.github.com/users/hadley/orgs', dataType: 'json', success: function (data) { console.log(data); }, }); }); });
Requesting the data through the api using an ajax function - Once done, right-click the Html code and select Open with Live Server.
Opening a file with a live server - If you cannot see this option, then install the Live Server extension and open the code on the browser.
Installing a Live Server extension - Once the browser is opened with the live server, right-click the empty space and select Inspect or press Ctrl + Shift + I together.
Opening an Inspect Window on the browser - Go to Console from the top and then click on Send request button to see if it works or not.
Calling an Api to receive the data - If it is working, you will see the data in the console, which is an output of the function that you have fetched through the API.
Hamza Mohammad Anwar
Hamza Mohammad Anwar is an intermediate JavaScript web developer with a focus on developing high-performance applications using MERN technologies. His skill set includes expertise in ReactJS, MongoDB, Express NodeJS, and other related technologies. Hamza is also a Google IT Certified professional, which highlights his competence in IT support. As an avid problem-solver, he recreates errors on his computer to troubleshoot and find solutions to various technical issues.
Back to top button