php - JSON.解析 : unexpected end of data at line 1 column 1 of the JSON data( Angular2 backend php server SQL)

标签 php mysql json angular

我在服务器的响应中收到这个(JSON.parse:JSON 数据第 1 行第 1 列的数据意外结束)错误。我为 JSON.stringify(user) 做了 console.log,它按原样显示了对象。甚至数据都存储到数据库中。 这是我的服务.ts

signup(user: User) {

    const body = JSON.stringify(user);

    const headers = new Headers({'Content-Type': 'application/json'});
    const requestOptions = new RequestOptions({method : RequestMethod.Post,headers : headers});
    return this.http.post('url', body,requestOptions)
        .map((response: Response) => response.json())


}

signin(user: User) {
    const body = JSON.stringify(user);
    const headers = new Headers({'Content-Type': 'application/json'});
    return this.http.post('url', body, {headers: headers})
        .map((response: Response) => response.json())

}

logout() {
    localStorage.clear();
}

isLoggedIn() {
    return localStorage.getItem('token') !== null;
}

这是我的php文件

<?php
include "db.php";
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$data = json_decode(file_get_contents("php://input"));
//print_r($data);
/*$firstname = mysql_real_escape_string($data->firstName);
$lastname = mysql_real_escape_string($data->lastName);
$email = mysql_real_escape_string($data->email);
$password = mysql_real_escape_string($data->password);*/

$sql = "INSERT INTO `tbl_user`(`firstname`, `lastname`, `email`,  `password`) VALUES                       ('$data->firstName','$data->lastName','$data->email','$data->password')";
if($data->firstName){
$qry = $conn->query($sql);
}

$conn->close();
?>

最佳答案

这是更新后的 php 文件

<?php
include "db.php";
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$data = json_decode(file_get_contents("php://input"));

$firstname = $mysqli->real_escape_string($data->firstName);
$lastname = $mysqli->real_escape_string($data->lastName);
$email = $mysqli->real_escape_string($data->email);
$password = $mysqli->real_escape_string($data->password);
$sql = "INSERT INTO `tbl_user`(`firstname`, `lastname`, `email`, `password`) VALUES ('$data->firstName','$data->lastName','$data->email','$data->password')";
if (mysqli_query($mysqli,$sql))
 {
 $arr = array('sucess' => "Records save");
 $jsn = json_encode($arr);
 print_r($jsn);
 }
 else 
 {
 $arr = array('failed' => "No records save", 'error' => '');
 $jsn = json_encode($arr);
 print_r($jsn);
 }

 ?>

关于php - JSON.解析 : unexpected end of data at line 1 column 1 of the JSON data( Angular2 backend php server SQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49704194/

相关文章:

mysql - 查看较早的查询日志 - MySQL

mysql - 如何编写用于分页的 sql 查询

ios - 从核心数据创建 json 字符串,反之亦然?

php - 用页码分页?

mysql - 为什么在 CakePHP 的查询结果中没有出现新的表列?

c# - 创建自定义 NewtonSoft JSON 转换器

json - 在 HTTP POST 请求中将 JSON 发送到 Slack

php - 如何在刷新后保持图像按钮状态

php 搜索框结果显示不正确?

php - CSS 布局和 <body> 标签不能一起工作并通过验证