Here down below is my code written in ajax which basically lets me fetch data from a fact api for my personnel project
Ajax code to change into fetch API code
`var limit = 3;
$.ajax({
method: 'GET',
url: 'https://api.api-ninjas.com/v1/facts?limit=' + limit,
headers: { 'X-Api-Key': 'YOUR_API_KEY'},
contentType: 'application/json',
success: function(result) {
console.log(result);
},
error: function ajaxError(jqXHR) {
console.error('Error: ', jqXHR.responseText);
}
});`
I have not really got any answer but I have tried to use this method below - please explain me in detail the whole concept.
Method 1 I tried was -
async function fetchData(target, options){ // creating a fetch request and awaiting it till the response comes
const fetch_data = await fetch(target, options);
// storing the value of the response into a variable named results;
const results = await fetch_data.json();
// running some tests to check the values
console.log(results);
}
// calling the function`
fetchData("https://api.api-ninjas.com/v1/facts?limit=1",
{ method: "GET",`
Headers:{ "Content-Type": "application/json",
'X-Api-Key': 'My api key'
}})