2016-12-28 33 views
1

我想要將一些數據發送到使用reduxpromise的php腳本,如下所示。PHP腳本無法從Redios中的axios的請求接收數據操作函數

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

但是php腳本無法接收數據。當我使用var_dump打印$_POST中的所有數據時。裏面什麼都沒有。

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

而且我得到了這樣的響應:

​​

我怎樣才能找回在PHP腳本中的數據?

+0

您確定要檢查正確的php文件嗎?如果您看到它在網絡標籤中發送,那麼沒有任何理由不應該發送。 –

+0

@RossWilson - 如果我修改腳本像添加'if(isset($ _ POST [「filePath」]))echo「yes」;其他回聲「不」;',我可以看到'不'。如果我添加'echo「,我就是正確的文件」;',我也可以在響應中看到它。 – Casper

+0

當你做'var_dump($ _ POST);'時,你看到了什麼?它看起來像axios正在發送一個JSON對象,所以你可能需要在$ _POST數組上運行json_decode。 –

回答

6

感謝馬特Altepeter和他的評論,我終於加入行解決:

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

所以,如果我做一個var_dump($_POST)現在,我可以得到的數據filePath

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