我得到了未定义的数据值。 这是我的代码。
parentComponent.ts
testMethod(){
console.log('t', this.answer.postRequest(this.quetion).subscribe(data => {
console.log('d',data);
})); //here i got undefined value to data
}
serviceProvider.ts
postRequest(ques:string):Observable<any>{
let header = new HttpHeaders({
'Content-Type' : 'application/json',"Accept":'application/json'
});
console.log(ques)
return this.http.post(
"http://localhost:8080/hello",
{'q': ques},
{headers: header}
).map(data => {
console.log(data);
}, // this work fine and value is assigned to data as expected
error => {(error);// Error getting the data});
}
答案 0 :(得分:0)
像这样使用
返回this.http.post(&#34; http://localhost:8080/hello&#34;,{&#39; q&#39;:ques},{header:header}) .map(data =&gt; {console.log(data);}) .map((resp:Response)=&gt; {return resp;});
它会有所帮助