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

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();
?>

我得到了这个(JSON.parse:JSON数据的第1行第1行意外的数据结束)错误 在我的服务器响应中。 我为JSON.stringify(用户)做了console.log,它显示了应该的对象。即使数据已存储到数据库。 这是我的service.ts p>

  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}); 
返回this.http.post('url',body,requestOptions)
 .map((response:Response)=&gt; response  .json())
 
 
} 
 
signin(用户:用户){
 const body = JSON.stringify(user); 
 const headers = new Headers({'Content-Type':'  application / json'}); 
返回this.http.post('url',body,{headers:headers})
 .map((response:Response)=&gt; response.json())
 \  n} 
 
logout(){
 localStorage.clear(); 
} 
 
 
NisLoggedIn(){
返回localStorage.getItem('token')!== null; 
} 
   code>  pre> 
 
 

这是我的php文件 p>

 &lt;?php 
include“db.php”; 
header(“Access-  C  ontrol-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-&gt  ; firstName); 
 $ lastname = mysql_real_escape_string($ data-&gt; lastName); 
 $ email = mysql_real_escape_string($ data-&gt; email); 
 $ password = mysql_real_escape_string($ data-&gt; password); *  / 
 
 $ sql =“INSERT INTO`tbl_user`(`firstname`,`lastname`,`email`,`password`)VALUES('$ data-&gt; firstName','$ data-&gt; lastName'  ,'$ data-&gt; email','$ data-&gt; password')“; 
if($ data-&gt; firstName){
 $ qry = $ conn-&gt; query($ sql); 
  } 
 
 $ conn-&gt; close(); 
?&gt; 
  code>  pre> 
  div>

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);
 }

 ?>