async function fetchdata(){
try {
const response = await fetch(url, options);
const result = await response.text();
console.log(result);
} catch (error) {
console.error(error);
}
}
just use this instead of the given try block it will work
Aşağıya yorum ekleyerek tartışmaya katılın:
I tried few solutions but now it is saying you are not subscribed to this api. i thought this was free api.
This code is not working
function getData()
{
const options = {
method: ‘GET’,
headers:
{
‘X-RapidAPI-Key’: ‘c74d28fc95msh0643089963e26c4p13a9adjsn4e3d51df13df’,
‘X-RapidAPI-Host’: ‘weather-by-api-ninjas.p.rapidapi.com’
}
};
fetch(‘https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=Karachi’)
.then(response => response.json())
.then(response => console.log(response))
.catch(response => response.error(err))
}
This is coorect workable code
async function fetchData() {
const url = ‘https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=Seattle’;
const options = {
method: ‘GET’,
headers: {
‘X-RapidAPI-Key’: ‘a173513e32msh17726ed826d53c8p179137jsnb02f328154f6’,
‘X-RapidAPI-Host’: ‘weather-by-api-ninjas.p.rapidapi.com’
}
};
try {
const response = await fetch(url, options);
const result = await response.text();
console.log(result);
} catch (error) {
console.error(error);
}
}
fetchData();
yes,After using all this , it is still not working
const url = ‘https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=Seattle’;
const options = {
method: ‘GET’,
headers: {
‘X-RapidAPI-Key’: ‘98fd28b3fdmsh8db458e9d48af2cp1f26c8jsn06daa50110eb’,
‘X-RapidAPI-Host’: ‘weather-by-api-ninjas.p.rapidapi.com’
}
};
async function fetchapi(){
try {
const response = await fetch(url, options);
const result = await response.text();
console.log(result);
} catch (error) {
console.log(error);
}
}
fetchapi();
(async () => {
try {
const response = await fetch(url, options);
const result = await response.json();
console.log(result);
} catch (error) {
console.error(error);
}
})();
try this
fetch(url, options)
.then(Response => Response.json())
.then(Response => (
console.log(Response)
))
.catch (err => console.error(err));
also after using this its not working.