Angular 5服务httpclient Post-未定义标题



我正在尝试发布来自Angular 5服务的数据。

在我的服务中:

导出类dataService { 标题:"我的标题";

然后

postIt() {
    return this.httpClient.post<any>('http://jsonplaceholder.typicode.com/posts', title: 'sometitle');
}

,然后从我的app.cmponent.ts oninit我有:

this.myDataService.postIt()
  .subscribe(
    res => {
      console.log(res);
    },
    err => {
      console.log("Error occured");
    }
  );

我遇到了错误:

 ERROR ReferenceError: title is not defined

我做错了什么?

发布请求接受第二个参数作为对象的JSON主体。当您尝试在帖子通话中发送任何数据时,它应该是对象格式

const body = {name: 'Brad'};
http
.post('/api/developers/add', body)
 // See below - subscribe() is still necessary when using post().
.subscribe(...);

以下是更多详细信息的链接 https://angular.io/guide/http

尝试这样:

postIt() {
    return this.httpClient.post<any>('http://jsonplaceholder.typicode.com/posts', { title: 'sometitle' });
}

最新更新