我正在尝试将JSON对象发布到Web服务。 这是我执行代码时得到的:
这是我的代码:
Component.ts
createPup() {
const newPup = {
name: this.name,
age: this.age,
breed: this.breed
};
this.__pupService.createPup(newPup).subscribe(
data => {
console.log(data);
return true;
},
error => {
console.error('Error Adding Pup!');
console.log(newPup);
return Observable.throw(error);
}
);
this.router.navigate(['/pups']);
}
Service.ts
createPup(newPup) {
const headers = new Headers({ 'Content-Type': 'application/json' });
const options = new RequestOptions({ headers: headers });
const body = JSON.stringify(newPup.data);
return this.http
.post(this._url, body, options)
.map((res: Response) => res.json()
);
}