我制作了一个天气应用程序,它通过 API 调用 freegeoip 来定位您当前位置的坐标,然后使用这些坐标连接到 openweathermap API 以获取您当前位置的天气。
在开发中,该应用程序运行良好。但是在部署到 Heroku 之后,我似乎得到了一个 CORS 错误?
控制台日志:
Mixed Content: The page at 'https://weather-react-drhectapus.herokuapp.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://freegeoip.net/json/'. This request has been blocked; the content must be served over HTTPS.
链接到Heroku 应用程序
编辑:
更改为https
似乎适用于 freegeoip API ( https://freegeoip.net/json/
),但不适用于 openweathermap API。这是我得到的完整控制台日志:
GET https://api.openweathermap.org/data/2.5/weather?appid=95108d63b7f0cf597d80c6d17c8010e0&lat=49.25&lon=4.0333 net::ERR_CONNECTION_CLOSED
bundle.js:16 Uncaught (in promise) Error: Network Error
at e.exports (bundle.js:16)
at XMLHttpRequest.d.onerror (bundle.js:16)
Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error
只需将 API 端点更改为使用https
而不是http
.
https://freegeoip.net/json/
效果很好 ;)
更新
您更新的问题还包含一个请求。不幸的是,api.openweathermap.org
无法通过 HTTPS 使用。因此,您需要在您的控制下通过代理访问它并将响应转发给您的客户端。有关更多信息,请参阅此答案
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句