当我发送POST请求时,我总是收到错误:&#34; JSON.parse:JSON数据的第1行第1列意外的数据结束&#34;,但数据将在数据库中创建< / p>
网络标签将显示状态代码201;当我按下修改&amp;重发,请求内容是有效的JSON
{&#34;的firstName&#34;:&#34; T&#34;&#34; lastName的&#34;:&#34; T&#34;&#34;密码&#34;:& #34; T&#34;&#34; confirmPassword&#34;:&#34; T&#34;&#34;一年&#34;:2018,&#34;一个月&#34;:2&# 34;天&#34;:28,&#34;电子邮件&#34;:&#34; T&#34;}
这是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();
});
堆栈跟踪:
错误:对象{headers:{...},状态:201,statusText:&#34; OK&#34;,url: &#34; https://music-makers.herokuapp.com/user/register&#34;,ok:false,name: &#34; HttpErrorResponse&#34;,消息:&#34;解析过程中的Http失败 https://music-makers.herokuapp.com/user/register&#34;,错误:{...}} register.ts:86:12
答案 0 :(得分:1)
发现它,后端需要发送204无内容响应而不是201.这是Angular中的一个错误
或者发送一个空的JSON,但我们没有对此进行测试。
答案 1 :(得分: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();
});