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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料