为什么我的PHP echo显示的是一个空的stringarray?

问题描述 投票:0回答:1

我目前正在开发一个React Native应用,它试图通过PHP向服务器发送JSON数据。它所做的一切都是在向我显示一个空字符串,我不明白为什么。当我试图在Android Studio中做类似的事情时,它也显示出一个空数组。以下是我的React Native代码(该应用使用Geolocation获取经纬度,并每隔10秒将其发送到服务器,供另一个应用从服务器上抓取并做一些事情)。

在这个函数中,我试图向服务器做一个POST请求。

sendDataToServer = () => {
var data = {
  "lat": this.state.where.latitude,
  "long": this.state.where.longitude
}

fetch('http://....sendData.php', {
  method: 'POST',
  headers: {
    'Accept' : 'application/json',
    'Content-Type' : 'application/json',
  },
  body: JSON.stringify(data)
}).then((response) => {
  return response.json()                            
  })
      .then((responseData) => {

      }).catch((error) => {
        console.error(error);
      });
}

这是我调用sendDataToServer()的函数。

geoSuccess = (position) => {
this.setState({
  ready:true,
  where: {latitude: position.coords.latitude, 
          longitude: position.coords.longitude}
});
this.setState({
  TextLat:
    this.state.where.latitude + " ",
  TextLong:
    this.state.where.longitude + " "
});
this.sendDataToServer();
}

这是我的PHP。

<?php
    $json = file_get_contents('php://input');
    echo json_encode($json);

当我运行这个函数时,它给我的是PHP中的""。我不知道是json_encode的问题还是我接收数据的方式的问题。当我尝试在json_encode之后使用echo json_last_error_msg()时,它在我的PHP文件中发出了 "No error",而我的React Native应用则给我一个错误信息说 "SyntaxError: JSON解析错误。Unable to parse JSON string"。任何帮助都是非常感激的

javascript php react-native server http-post
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.