首页 文章

IONIC - POST JSON.parse:JSON数据的第1行第1列的意外数据结束

提问于
浏览
0

当我发送POST请求时,我总是收到错误:“JSON.parse:JSON数据的第1行第1列意外的数据结束”,但数据将在数据库中创建

网络选项卡将显示状态代码201;当我按下修改并重新发送时,请求内容是一个有效的JSON

{“firstName”:“t”,“lastName”:“t”,“password”:“t”,“confirmPassword”:“t”,“year”:2018,“month”:2,“day”: 28, “电子邮件”: “T”}

这是POST请求:

let options = {
  headers: new HttpHeaders({'Content-type': 'application/json', 'Accept': 'application/json, */*'})
};

let registerUser = <RegisterUser>({
  firstName: this.model.firstName,
  lastName: this.model.lastName,
  password: this.model.password,
  confirmPassword: this.model.confirmPassword,
  year: parseInt(this.birthday.split('-')[0], 10),
  month: parseInt(this.birthday.split('-')[1], 10),
  day: parseInt(this.birthday.split('-')[2], 10),
  email: this.model.email
});

this.http.post(this.registerUrl, registerUser, options)
        .map((res: Response) => res.json())
        .subscribe(data => {
            console.log("subscribe: " + data);
          },
          error => {
            console.log("error: ", error);
          },
          () => {
            console.log("Complete");
            this.navCtrl.pop();
          });

和堆栈跟踪:

error:Object {headers:,status:201,statusText:“OK”,url:“https://music-makers.herokuapp.com/user/register”,ok:false,name:“HttpErrorResponse” ,消息:“解析https://music-makers.herokuapp.com/user/register时出现Http失败”,错误:} register.ts:86:12

2 回答

  • 0

    尝试删除块:

    .map((res: Response) => res.json())
    

    改变后:

    this.http.post(this.registerUrl, registerUser, options)
        .subscribe(data => {
            console.log("subscribe: " + data);
          },
          error => {
            console.log("error: ", error);
          },
          () => {
            console.log("Complete");
            this.navCtrl.pop();
          });
    
  • 0

    发现它,后端需要发送204无内容响应而不是201.这是Angular中的一个错误

    或者发送一个空的JSON,但我们没有测试这个 .

相关问题