获取错误:除非页面刷新,否则无法加载资源 - React

时间:2021-06-30 23:13:30

标签: reactjs fetch use-effect

我创建了一个 fetch 调用:

select *,
       (case when CONVERT(varchar,date_time,14) between '00:00:00.000' and '12:00:00.000'
            then '0-12' else date_time end) as 'TIME'

控制台出现无法访问资源的错误 但是当我刷新时,我可以看到网址。

1 个答案:

答案 0 :(得分:0)

尝试将 fetch 调用包装在这样的函数中:

    fetch(testURL)
      .then(function(response) {
        return response.json();
      })
      .then(function(data) {
        console.log(JSON.stringify("test data", data));
      })
      .catch(() => {
        console.log(Error);
      });

然后在 useEffect 中调用函数:

  const fetchTest = () => {
    fetch(testURL)
      .then(function(response) {
        return response.json();
      })
      .then(function(data) {
        console.log(JSON.stringify("test data", data));
      })
      .catch(() => {
        console.log(Error);
      });
  };

这将确保无需先刷新即可加载资源。

相关问题