Advertisements
I’m sorry for a beginner question, this is my first time trying API calls and I encountered an error, it says "$ is not defined", I tried adding name after the dollar sign but still, again sorry for beginner question.
here’s my code:
var limit = 2;
$.ajax({
method: 'GET',
url: 'https://api.api-ninjas.com/v1/facts?limit=' + limit,
headers: { 'X-Api-Key': 'MY API KEY'},
contentType: 'application/json',
success: function(result) {
console.log(result);
},
error: function ajaxError(jqXHR) {
console.error('Error: ', jqXHR.responseText);
}
});
>Solution :
Did you import jQuery correctly?
This Syntax is part of jQuery
EDIT: for i.e. jQuery 3.6.0 just include before your <script>
the following line:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Resource: Google hosted libraries