我有一個使用React Native構建的應用程序,它在我的本地電腦上運行,我想從我正在運行的本地Symfony API中獲取和顯示數據。
React Native程式碼正在從我的本地PC IP和Symfony連接埠/路由中取得:
constructor(props) { super(props); this.state = { isLoading: true, dataSource: [], } } componentDidMount() { return fetch('http://10.161.170.86:8000/api/hardwarePlacement') .then((response) => { console.log(response.ok); }) .then((response) => response.json()) .then((responseJson) => { console.log(response.ok); this.setState({ isLoading: false, dataSource: responseJson.hardwarePlacements, }) }) .catch((error) => { console.log(error) }); }
從我的Symfony API取得的JSON資料如下,當我透過Postman或直接透過瀏覽器取得:
[{"id":1,"name":"Bryggers","createdDate":{"date":"2023-02-08 15:14:12.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"editedDate":{"date":"2023-02-14 13:57:07.000000","timezone_type":3,"timezone":"Europe\/Berlin"}},{"id":2,"name":"Stue","createdDate":{"date":"2023-02-08 21:52:46.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"editedDate":{"date":"2023-02-08 21:52:46.000000","timezone_type":3,"timezone":"Europe\/Berlin"}},{"id":3,"name":"Stue","createdDate":{"date":"2023-02-14 13:57:10.000000","timezone_type":3,"timezone":"Europe\/Berlin"},"editedDate":{"date":"2023-02-14 13:57:10.000000","timezone_type":3,"timezone":"Europe\/Berlin"}}]
我在終端機中得到的錯誤是:
[TypeError: undefined is not an object (evaluating 'response.json')]
如果我嘗試從公共URL獲取數據,它可以正常工作,只有從本地主機URL獲取數據時會失敗。
從第一個
then
回傳