React Native:在访问本地主机Symfony API时遇到未定义的响应问题
P粉094351878
P粉094351878 2023-09-13 22:41:56
0
1
570

我有一个使用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粉094351878
P粉094351878

全部回复(1)
P粉333186285

从第一个then返回

.then((response) => {
    console.log(response.ok);
    return response
  })  
  .then((response) => response.json())
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板