Fetch returning 404 error: "GET <urlthatIwasfetchinghere> 404 (Not Found)"

Fetch returning 404 error: "GET <urlthatIwasfetchinghere> 404 (Not Found)"

我正在使用 fetch,据我了解,从不存在的页面中提取时应该会出现 404 代码,但不会出现 GET 404 错误。我正在使用开放天气图 api。我得到的错误是 GET ${forcastUrl} 404 (Not Found).

我的获取语句如下所示:

function fetchingForcast(forcastUrl){
    fetch(forcastUrl)
    .then(validateWeatherResponse)
    .then(responseToJSON)
    .then(dat => {
      forcastDat = dat;
      updateforcastPanel();
    })
    .catch(error=>{
      console.log(error);
    })
  }
function validateWeatherResponse(response){
  if (response.ok){
    return response;
  } else {
    throw Error("response.ok: "+ response.ok);
  }

validateWeatherResponse 检查响应是否正确,如果不正确,则抛出错误。但是,当它确实抛出错误时,它应该去捕获并 console.log 错误。当我在 chrome 上使用调试开发工具时,错误似乎在调用 validateWeatherResponse 时立即发生。

除了这个错误,当我输入正确的网站时,我的代码可以正常工作。据我了解,不应发生 404 错误,因此我想了解为什么会发生这种情况。 谢谢!

来自MDN's HTTP status code documentation

404 Not Found The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. This response code is probably the most famous one due to its frequent occurrence on the web.

404 错误代码表示您请求的网站或端点不存在,如果您请求的页面不存在,这绝对是预料之中的。

如果您仍然对错误感到困惑,请在评论中告诉我。