Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post data in php via react js

I am new to react js please see below my post API call in react

updateData = datas => {
    axios
      .post("http://localhost:8080/ReactAPI/post.php", datas)
      .then(function(body) {
        console.log(body);
      })
      .catch(function(error) {
        console.log(error);
      });
}

and PHP file post.php code given below:

<?php

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, X-Requested-With");

print_r($_POST);
?>

console i give response body just like given below:

{data: "Array↵(↵)↵", status: 200, statusText: "OK", headers: {…}, config: {…}, …}

I pass datas given below to i aspect in response

{id: "41", userId: "3672367892", fullName: "Subhash Patel"}

Please help me out here how to recieve the datas response in post.php file

like image 378
Subhash Patel Avatar asked Oct 09 '18 09:10

Subhash Patel


1 Answers

You can't get json POST data by $_POST variable. Please use following:

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
like image 65
ahadortiz Avatar answered Oct 07 '22 00:10

ahadortiz