从本机应用程序发送数据到远程php服务器时出错

时间:2018-05-03 05:57:49

标签: php android react-native

我正在尝试将我的react本机应用程序中的数据发送到用PHP编写的远程应用服务器,以下是代码片段:

fetch('http://172.20.10.2/fyp/products.php', {
  method: 'POST',
  body: JSON.stringify({
  //passing input data to server
    cid: this.state.category_id

  })

这是我试图从我的本机应用程序接收数据的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文件,但它没有打印任何表明我没有收到服务器中的数据的文件。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在你的php文件的最顶部添加

header(“Access-Control-Allow-Origin:*”); header(“Access-Control-Allow-Headers:*”);

再试一次!