console.log("data ", data); // returns an object in JSON format {propertyName: propertyValue}
dataString = JSON.stringify(dataString); //correctly stringified json
let response = await fetch('updateRecevingEntry.php',
{
method:'POST',
headers: {'Content-Type':'application/json'},
body: dataString
}).then(response=>response.json());
但是我在 php 端返回了一个未定义的索引。
php在哪里:
$matkey = $_POST['materialKey'];
返回
<b>Notice</b>: Undefined index: materialKey in <b>path/updateRecevingEntry.php</b> on line <b>3</b><br />
对于所有数据......没有一个被抓住。
那么为什么 _POST['propertyName'] 没有从正文中捕获 stringData 呢?
我尝试了一些变体,例如发送数据而不是与标头混淆的字符串数据,但我似乎无法弄清楚如何发送有效负载以便 _POST['propertyName'] 捕获数据身体。
我以前在 jquery 中使用过 $.ajax,它正在工作:但我正在重构它。
Fetch api对我来说是新的。我哪里错了。我也不想在 php 端解析 json 对象。
在阅读了一个答案后,我得到了它在一种情况下的工作,但是
let response = await fetch('updateRecevingEntry.php',
{
method:'POST',
headers: {'Content-Type':'application/json'},
body: sendData
}).then(response=>response.json());
和PHP
$postData = json_decode(file_get_contents("php://input"), true);
var_dump($postData);
只是返回一个大胖NULL。
编辑二:原来它实际上只需要通过 JSON.stringify(sendData) 进行编码。自从。它按预期工作。
慕仙森