Cas*_*per 1 php redux redux-promise axios
我想通过使用redux和发送一些数据到PHP脚本,promise如下所示.
export function fetchFileContent() {
return {
type: "FETCH_FILECONTENT",
payload: axios.post("/api/ide/read-file.php", {
filePath: document.getArgByIndex(0)[0]
})
};
}
Run Code Online (Sandbox Code Playgroud)
但是php脚本无法接收数据.当我打印的所有数据$_POST使用var_dump.里面什么都没有.
我在Google Chrome调试工具中查看了" 请求有效负载 ",这似乎没问题.

在我的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'];
Run Code Online (Sandbox Code Playgroud)
我收到了这个回复:
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 />
Run Code Online (Sandbox Code Playgroud)
如何在php脚本中获取数据?
感谢Matt Altepeter和他的评论,我终于通过添加以下内容解决了问题:
$_POST = json_decode(file_get_contents('php://input'), true);
Run Code Online (Sandbox Code Playgroud)
所以,如果我var_dump($_POST)现在做,我可以得到数据filePath.
array(1) {
["filePath"]=>
string(10) "/I am here"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2977 次 |
| 最近记录: |