PHP脚本无法从Redux操作函数中的axios请求接收数据

时间:2016-12-28 19:08:33

标签: php redux axios redux-promise

我希望使用reduxpromise将一些数据发送到php脚本,如下所示。

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

但是php脚本无法接收数据。当我使用$_POST打印var_dump中的所有数据时。里面什么都没有。

我检查了Google Chrome调试工具中的请求有效负载,这似乎没问题。 enter image description here

在我的php脚本中:

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'];

我收到了这个回复:

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 />

如何在PHP脚本中取回数据?

1 个答案:

答案 0 :(得分:9)

感谢Matt Altepeter和他的评论,我终于通过添加以下行解决了问题:

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

因此,如果我现在var_dump($_POST),我可以获取数据filePath

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