【问题标题】:Getting Error: Network Error at e.exports (createError.js:16) at XMLHttpRequest.g.onerror (xhr.js:117) for API call出现错误:用于 API 调用的 XMLHttpRequest.g.onerror (xhr.js:117) 处的 e.exports (createError.js:16) 网络错误
【发布时间】:2022-01-02 00:26:09
【问题描述】:

我正在尝试通过 Axios 发出 API 请求,但收到此错误。

Error: Network Error
    at e.exports (createError.js:16)
    at XMLHttpRequest.g.onerror (xhr.js:117)

代码如下:

    async function getData() {
  await axios
    .get(
      "api.openweathermap.org/data/2.5/weather?q={city name}&appid={api key}"
    )
    .then(function (data) {
      console.log(data);
    })
    .catch(function (err) {
      console.log(err);
    });
}

我该如何解决这个问题?

谢谢

【问题讨论】:

  • 需要更多细节。您遇到的网络错误是什么?例如403? 404?别的东西?它是否带有错误消息?
  • 我也得到了这个:CORS 策略已阻止从源“null”访问 XMLHttpRequest:跨源请求仅支持协议方案:http、data、chrome、chrome-extension、chrome - 不受信任,https。

标签: javascript api axios weather openweathermap


【解决方案1】:

尝试提及正确的协议。

"https://api.openweathermap.org/..."

确保您使用的是正确的 URI 路径。还要检查您的连接性。 err 的值可能也值得研究。

【讨论】:

  • 哦,我在 URL 中缺少 HTTP 协议,我很傻。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多