1 回答

TA貢獻1827條經驗 獲得超8個贊
我注意到的第一件事是您沒有使用正確的變量(您正在使用stringData而不是dataString):
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());
盡管您在使用 json 標頭發送它時不需要對其進行字符串化。
此外,您是否嘗試過$_POST使用 ,而不是php://input?
來自PHP.net:
php://input 是一個只讀流,允許您從請求正文中讀取原始數據。對于 POST 請求,最好使用 php://input 而不是 $HTTP_RAW_POST_DATA,因為它不依賴于特殊的 php.ini 指令。此外,對于那些默認情況下不填充 $HTTP_RAW_POST_DATA 的情況,與激活 always_populate_raw_post_data 相比,它可能是一種內存密集度較低的替代方案。php://input 不適用于 enctype="multipart/form-data"。
所以你會像這樣使用它:
$postData = json_decode(file_get_contents("php://input"), true);
$matkey = $postData['materialKey'];
這會將 POST 請求的主體讀取為 JSON 字符串,然后將其轉換為 PHP 數組。
- 1 回答
- 0 關注
- 75 瀏覽
添加回答
舉報