TypeError: $.getJSON is not a function in jQuery, a common error you often get when working with jQuery functions. Our explanations below will help you solve this kind of error with causes and solutions.
What causes the TypeError: $.getJSON is not a function in jQuery?
In fact, getJSON() is a method which is included in many jQuery libraries. This method helps you load the JSON-encoded data from the server via a HTTP GET request. The TypeError: $.getJSON is raised implies that this method getJSON() is not found in the jQuery version that you have imported, it might be due to you have used the old versions or jQuery slim versions in the HTML file. For instance:
Have you acknowledged the cause of the error? It’s because of the version of jQuery you have used, which in the above example is: 3.3.1.slim.min.js. It is a jQuery slim version so it does not include the getJSON method or many ajax functions.
To solve the error, we recommend you use a slim version or a full one or use a newer version of jQuery. Follow the next section to see how to solve this problem.
How to fix this error?
Use the jQuery full version
You should remember that this function getJSON() only works if you are using a full version or min version of jQuery but not the slim one. As the slim one excluded this function and some unused ones. Therefore you should use the full jQuery version, for example:
Use the jQuery min version
In the previous explanations, the getJSON() method may not present in jQuery slim versions which you imported. However, it is included in min jQuery versions and also the full jQuery version (as previous). That is the reason why you can use the jQuery min version too:
As can be seen, we have imported the jQuery min version as in the above example. To be honest, although there are many versions that include this getJSON function, we suggest you use the newest version instead because it can help you prevent some other errors while using it. Therefore, if you make changes to the version you used, the results can become different from what expected.
We discussed many ways to fix the TypeError: $.getJSON is not a function in jQuery. We suggest you import only one version of jQuery in your code and make sure it is the newest slim or full version, you can easily solve this problem with our tutorials.
Maybe you are interested:
- How To Solve TypeError: $(…).datepicker Is Not A Function In jQuery?
- Solution To TypeError: $(…).DataTable Is Not A Function Error In JQuery
- TypeError: $(…).animate is not a function in jQuery
Name of the university: HCMUT