Angular 6/PHP Http 在解析过程中失败



我目前正在学习角度 6,但与数据库的连接出现问题。

addData(nachname: any, vorname: any, telefon: any) {
return this.client.post('http://localhost/api/add.php', {
nachname,
vorname,
telefon
}).subscribe((data) => {
console.log(data);
});

}

<?php
header("Access-Control-Allow-Credentials: true"); 
header('Content-type: application/json');
header("Access-Control-Allow-Origin: ".((isset($_SERVER['HTTP_ORIGIN'])) ?                
$_SERVER['HTTP_ORIGIN'] : "*"));
header('Access-Control-Allow-Headers: X-Requested-With, content-type,           
access-control-allow-origin, access-control-allow-methods, access-control- 
allow-headers');
$db = mysqli_connect("localhost", "root", "", "telefonauskunft");
if(!$db){
exit("Verbindungsfehler: ".mysqli_connect_error());
}
$db -> set_charset('utf8');
$model = json_decode(file_get_contents("php://input"));
$sql = "Insert Into telefonauskunft ('Nachname', 'Vorname', 'Telefon')
Values ('$model -> Nachname', '$model -> Vorname', '$model ->           
Telefon');";
if($model->Nachname){
$qry = $db->query($sql);
}
$db->close();
?>

这些是我的代码。我得到了一个正常的选择查询 this.client.get (...(。

不幸的是,我得到了这个代码

错误 : {error: SyntaxError: 在 XMLHttp 的 JSON.parse (( 位置 0 处的 JSON 中意外的令牌<...,

解析期间的 HTTP 失败

我的问题在哪里?我提前感谢您的回答和帮助

您应该在 PHP 文件中打印出一些内容,例如一个布尔值,说明数据库查询成功。理想情况下,您应该遵循常见的 JSON API 准则,参见 http://jsonapi.org/format/upcoming/

print json_encode($db->query($sql));

最新更新