Javascript 如何在react native中从php获取json
我有一个PHP文件。当我使用HTML JS中的POST方法发送Javascript 如何在react native中从php获取json,javascript,android,reactjs,react-native,Javascript,Android,Reactjs,React Native,我有一个PHP文件。当我使用HTML JS中的POST方法发送category:'true'时,它会返回我在PHP中回显的文本,但使用React Native它会向我发送一个对象。我怎样才能解决这个问题 这是我的代码: return fetch('http://709957ef.ngrok.io/follower/get%20json.php',{ method:'POST', body:JSON.stringfy({ category:
category:'true'
时,它会返回我在PHP中回显的文本,但使用React Native它会向我发送一个对象。我怎样才能解决这个问题
这是我的代码:
return fetch('http://709957ef.ngrok.io/follower/get%20json.php',{
method:'POST',
body:JSON.stringfy({
category:'true'
})
})
.then((response) => response.text())
.then((responsetxt) => {
alert(responsetxt)
})
.catch((error) => {
console.error(error);
});
}
将代码更改为,并将异步添加到我们的函数中
componentDidMount = async () => {
const response = await fetch('http://709957ef.ngrok.io/follower/get%20json.php',{
method:'POST',
body:JSON.stringify({
category:'true'
})
})
this.setState({
data: response.text(),
}, () => console.log('PHP DATA: ', this.state.data))
}
你是说
responseText
是一个对象吗?是的,当我提醒这个安卓说:[object object]时,你只需要访问它上的一个属性,这就是你要寻找的值。不确定response.text()返回另一个承诺的原因。我将代码更改为:----async componentDidMount(){const response=await fetch(“”,{method:'POST',body:JSON.stringfy({category:'true'})return await(response.text());}----但此代码不起作用,react本机警告为:可能未处理的promisw拒绝(id:0):typeError:Undefined不是函数(评估'json.stringfy({category:'true'}))“不要从那里回来,把它保存在状态中。请使用我更新的答案。我已更改但未支持任何内容,状态未更改,警告未解决。您得到了什么?这是警告图片: