【问题标题】:error in sending data to remote php server from react native app从反应本机应用程序向远程 php 服务器发送数据时出错
【发布时间】: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


【解决方案1】:

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

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

再试一次!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多