【发布时间】:2020-01-28 13:06:20
【问题描述】:
我正在尝试从元天气 API 获取数据。由于 CORS 问题,我正在使用这个名为“crossorigin.me”的代理。仍然,它不让我获取数据。在许多人建议这样做之后,我什至包括了“模式:'no-cors”。
<!DOCTYPE html>
<html>
<head>
<title> Fetch Promise </title>
</head>
<body>
<script>
function getWeather(woeid){
fetch(`https://crossorigin.me/https://www.metaweather.com/api/location/${woeid}/`,{mode: 'no-cors'})
//fetch always returns a promise
.then(data => {
console.log(data)
return data.json()
// json also returns a promise so we handle that by chaining
})
.then(result => {
const today = result.consolidated_weather[0]
console.log(`temperature in ${result.title} stay between ${today.min_temp} and ${today.max_temp}`)
})
.catch(error => console.log(error))
}
getWeather(2487956)
</script>
</body>
</html
【问题讨论】:
-
检查浏览器开发工具网络中的实际请求。我得到一个 522 状态。然后,您可以检查响应正文并阅读原因。简而言之,那个代理服务似乎不可靠
标签: javascript cors