gpt4 book ai didi

javascript - 如何在 React Native 中从 PHP 获取 json

转载 作者:行者123 更新时间:2023-11-29 23:06:38 25 4
gpt4 key购买 nike

我有一个 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:'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))
}

关于javascript - 如何在 React Native 中从 PHP 获取 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56434874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com