首页 > 解决方案 > fetch 无法正常工作且 console.log 未显示

问题描述

我目前正试图弄清楚为什么我的 .json 文件无法获取。即使我在 fetch 中输入随机字母,它也不会给我任何错误。目前使用 my-react-app。

constructor(props) {
  super(props);
  this.state = {
    productData: [],
  }
}

ComponentDidMount() {
  this.fetchData();
}

fetchData() {
  fetch('./ProductInformation.json')
    .then(Response => Response.json())
    .then(parsedJSON => console.log(parsedJSON.results))
    .catch(error => console.log('parsing failed', error))
}

标签: javascriptreactjs

解决方案


推荐阅读