fetch() php,PHP:使用fetch()发送数据
我試圖使用fetch()發送一些數據,但作為回報,我得到
SyntaxError:意外的標記,在JSON中的位置23
這就是我要做的
fetch('/api.php', {
method: 'POST',
body: JSON.stringify({
nom : "Issa",
prenom: "Oule"}),
headers : {"Content-Type" : "application/json"},
})
.then(res => res.json())
.then((res) => {
console.log(res);
})
.catch((err) =>{
console.log(err);
})
在我的php文件中:
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if($contentType === "application/json") {
$content = trim(file_get_contents("php://input"));
$decoded = json_decode($content, true);
if(! is_array($decoded)) {
echo '{"status":"error decoding"}';
} else {
echo '{"status":"ok", "Nom": '. $decoded['nom'];.', "Prenom": '.$decoded['prenom'].'}';
}
}else{
echo '{"status":"error data"}';
}
?>
有人能幫我嗎
提前謝謝
總結
以上是生活随笔為你收集整理的fetch() php,PHP:使用fetch()发送数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈密瓜用保鲜膜封上第二天会坏吗
- 下一篇: 腊肠怎么保存 腊肠保存的正确方法?