我打算使用HttpClient發佈到我的getFeedback終端並獲取響應並在我的應用中顯示它。 我有一個codeService和一個儀表盤組件,這是在我的代碼服務崗位代碼:Angular HttpClient無法獲得響應正文消息
//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(數據))'的結果呢? – mok
兩者都返回null –
這可能是一個錯誤。請檢查您使用的網站的域名和api是否相同。 – TheChetan