Angular 2+:错误POST http:// localhost:3000 / api / pups / 500(内部服务器错误)

时间:2018-05-16 09:08:51

标签: angular http post service routing

我正在尝试将JSON对象发布到Web服务。 这是我执行代码时得到的: enter image description here

这是我的代码:

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

0 个答案:

没有答案