2021年2月7日星期日

Time API could be use on local server, but not when it is on CodeSandbox.io?

For the code:

export default function App() {    const [data, setData] = useState({});      useEffect(() => {      fetch("http://worldclockapi.com/api/json/pst/now")        .then(res => res.json())        .then(dataFetched => {          setData(dataFetched);        });    }, []);      return (      <div className="App">        <pre className="data">          {data.currentDateTime}        </pre>       </div>    );  }  

Example: https://codesandbox.io/s/shy-snowflake-1vteb

I was able to run it as a local app on http://localhost:3001 using npm start, but if it is used on https://codesandbox.io like above, the fetch would fail. The same if I use the other API http://worldtimeapi.org/api/timezone/America/Los_Angeles

Anybody know why and how to make it work?

https://stackoverflow.com/questions/66095426/time-api-could-be-use-on-local-server-but-not-when-it-is-on-codesandbox-io February 08, 2021 at 11:06AM

没有评论:

发表评论