【发布时间】:2017-09-08 04:02:17
【问题描述】:
我计划使用 HttpClient 发布到我的 getFeedback 端点并获得响应并将其显示在我的应用程序中。 我有一个 codeService 和一个仪表板组件,这是我的代码服务中发布的代码:
//method 1
async generateFeedbackPost() {
var headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
var body = new URLSearchParams();
body.set('code', this.code);
return this.http.post(this.url, body.toString(), { headers }).toPromise();
}
//method 2
generateFeedbackPost2() {
var headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
var body = new URLSearchParams();
body.set('code', this.code);
return this.http.post(url, body.toString(), {headers});
}
在我的仪表板组件中,我调用了这些方法:
this.codeService.generateFeedbackPost2().subscribe(data => {console.log(data);});
this.codeService.generateFeedbackPost().then(data => {console.log(data);});
我已经尝试了这两种方法,但是它们都在控制台中打印 null。但是,在我的 chrome 开发者工具网络选项卡中,我可以准确地看到结果:
你知道如何找回这些消息吗?
【问题讨论】:
-
console.log(JSON.stringify(data))的结果是什么? -
都返回null
-
可能是 cors 错误。请检查您使用的网站域名和api是否相同。
-
你想使用像
/xxx?code=yyy这样的网址 -
谢谢大家,我已经修好了。我忘记将我的响应更改为 json 格式
标签: angular post httpclient