我有一个使用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获取数据时会失败。
P粉3331862852023-09-14 11:29:53
从第一个then
返回
.then((response) => { console.log(response.ok); return response }) .then((response) => response.json())