简体   繁体   中英

How can I use react-native with php with fetch return data is always null

My return is always null. i cant seems to get this to work. How can I use react-native with php with fetch json. anyone can help?

PHP

$myArray = array();
$myArray['lat'] = $_POST['lat']; 
$myArray['lng'] = $_POST['lng'];
echo $_POST['lat'];

React-Native Fetch

fetch(url, { 
  method: 'post',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    lat: region.latitude,
    lng: region.longitude
  })
})
.then((response) => response.json())
.then((responseData) => {
  console.log(responseData);
    })
    .catch((error) => {
      console.warn(error);
    })
    .done();

Have to use file_get_contents('php://input') if not php cant see the data.

$json = json_decode(file_get_contents('php://input'), true);

$myArray = array();
$myArray['lat'] = $json['lat']; 
$myArray['lng'] = $json['lng'];
$myArray['status'] = 'success';
echo json_encode($myArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM