donpvtzuux37724 2018-04-07 05:29
浏览 100
已采纳

JSON.parse:JSON数据第1行第1列的意外数据结尾(Angular2后端php服务器SQL)

I am getting this(JSON.parse: unexpected end of data at line 1 column 1 of the JSON data )error in my Response from the server. I did console.log for JSON.stringify(user) and it shows the object as it should be.Even the data is been stored to database. this is my service.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;
}

this is my php file

<?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();
?>
  • 写回答

1条回答 默认 最新

  • dongli4711 2018-04-07 07:04
    关注

    heres the updated php file

    <?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);
     }
    
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系