Javascript 从metaweather API获取数据以响应页面
我试图使用这个metaweather API,但它不起作用。这是一个react项目,我正在使用localhost。请告诉我我做错了什么Javascript 从metaweather API获取数据以响应页面,javascript,reactjs,fetch,Javascript,Reactjs,Fetch,我试图使用这个metaweather API,但它不起作用。这是一个react项目,我正在使用localhost。请告诉我我做错了什么 const fetchWeatherData = async() =>{ fetch('https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02', { method:'GET', mode:'no-cors', headers: {
const fetchWeatherData = async() =>{
fetch('https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02', {
method:'GET',
mode:'no-cors',
headers: {
'Content-Type': 'application/json',
},
})
.then(response => {
console.log('response',response);
return response.json();
})
.then(data => {
console.log('data',data);
})
.catch((err) => {
console.log(err)
})}
这些是我的日志
您没有用花括号关闭函数,我已经测试过了,它工作正常,只需调用函数
fetchWeatherData
const fetchWeatherData=async()=>{
取('https://www.metaweather.com/api/location/search/?lattlong=36.96,-122.02', {
方法:'GET',
模式:'no-cors',
标题:{
“内容类型”:“应用程序/json”,
},
})
。然后(响应=>{
返回response.json();
})
。然后(数据=>{
console.log('data',data);
})
.catch((错误)=>{
console.log(错误)
})
}
fetchWeatherData()
很抱歉,我没有找到右括号,但我尝试了你的代码。其结果相同@AIANCOLD您能再次发送错误吗?是的,我做了@AianSyntaxError:输入和响应意外结束。状态=0@Aianaa,您应该在服务器端调用它,因为MetaWeather API不支持CORS,请检查这一个