【发布时间】:2018-05-03 05:57:48
【问题描述】:
我正在尝试将数据从我的 react 本机应用程序发送到用 PHP 编写的远程应用程序服务器,这是代码 sn-p 它:
fetch('http://172.20.10.2/fyp/products.php', {
method: 'POST',
body: JSON.stringify({
//passing input data to server
cid: this.state.category_id
})
这是我试图从我的 react 本机应用程序接收数据的 PHP 代码:
// Getting the received JSON into $json variable.
$json = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
$obj_id = $obj['cid'];
为了调试,我正在将$obj_id 打印到一个 .txt 文件中,但它没有打印任何内容,这表明我没有在我的服务器中接收数据。我该如何解决这个问题?
【问题讨论】:
-
请添加 JSON 数据类型标头以发送 json `标头:{ Accept: 'application/json', 'Content-Type': 'application/json', },`
标签: php android react-native