Search code examples
phpreduxaxiosredux-promise

PHP Script Cannot Receive Data from axios's Request in Redux Action Function


I want to sent some data to a php script with the use of redux and promise as if the following.

export function fetchFileContent() {
    return {
        type: "FETCH_FILECONTENT",
        payload: axios.post("/api/ide/read-file.php", {
            filePath: document.getArgByIndex(0)[0]
        })
    };
}

But the php script cannot receive the data. When I print all the data in $_POST using var_dump. There is nothing inside.

I checked the Request Payload in the Google Chrome debugging tool and it seems no problem. enter image description here

In my php script:

if (isset($_POST["filePath"])) 
    echo "yes"; 
else 
    echo "no";
echo "I am the correct file";
var_dump($_POST["filePath"]);

$dir = $_POST['filePath'];
echo $_POST['filePath'];

And I got this response:

noI am the correct file<br />
<b>Notice</b>:  Undefined index: filePath in <b>/var/www/html/api/ide/read-file.php</b> on line <b>7</b><br />
NULL
<br />
<b>Notice</b>:  Undefined index: filePath in <b>/var/www/html/api/ide/read-file.php</b> on line <b>9</b><br />
<br />
<b>Notice</b>:  Undefined index: filePath in <b>/var/www/html/api/ide/read-file.php</b> on line <b>10</b><br />

How can I get back the data in the php script?


Solution

  • Thank to Matt Altepeter and his comment, I finally solved by adding the line:

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

    So if I do a var_dump($_POST) now, I can get the data filePath.

    array(1) {
      ["filePath"]=>
      string(10) "/I am here"
    }