我试图使用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"}';
}
?>
有人能帮我吗
提前谢谢