angular2-rc1 http.post不能正常工作

我正在创build一个angular2应用程序,我在我的服务中使用http进行POST调用到mongoDB

当我第一次进行POST调用时,它工作正常,即条目插入到数据库正确,但是当我第二次发送数据时,它不被插入。

如果我第一次插入后重新加载页面,那么它的工作正常。

我做了一些debugging,发现在第二次请求时,我的req.body是空白的。

这是我的代码:

page.service.ts

 savePage(page: Object) { this.headers.append('Content-Type', 'application/json'); let url = this.baseUrl+'/pm/pages/'; let data={}; data["data"]=page; console.log(data); //this is printing both times correctly //on second request data is blank where as it works correctly for first time return this.http.post(url, JSON.stringify(data),{headers: this.headers}) .map((res: Response) => res.json()).catch(this.handleError); } 

这是我在节点服务中显示的req.body中的数据。

首要要求

 body:{ data:{ name: 'wtwetwet', desc: 'wetwetwetetwte', isPublic: true, createdBy: 'Bhushan' } } 

第二个请求

 body: {} 

任何投入?

它看起来更像是一个后端的东西。 然而,你应该包括在这个http调用中订阅的代码。

顺便说一句,你为什么使用RC1? Angular 2现在在RC5上。

我终于意识到,我的方法用于设置内容types每次被调用。

从而改变代码:

 savePage(page: Object) { this.headers.append('Content-Type', 'application/json'); let url = this.baseUrl+'/pm/pages/'; let data={}; data["data"]=page; return this.http.post(url, JSON.stringify(data),{headers: this.headers}) .map((res: Response) => res.json()).catch(this.handleError); } 

至:

 savePage(page: Object) { this.headers=new Headers(); this.headers.append('Content-Type', 'application/json'); let url = this.baseUrl+'/pm/pages/'; let data={}; data["data"]=page; return this.http.post(url, JSON.stringify(data),{headers: this.headers}) .map((res: Response) => res.json()).catch(this.handleError); } 

为我做了诡计。 实际上,我们只需要在设置标题时只设置一次,但是在我的代码中已经设置好了,因此创build了新的标题对象帮助我清除了以前设置的configuration。

谢谢你们的帮助