问题描述
使用具有以下数据的 Angular 10 和 HttpClient:
const data = {
'username': username,'password': password
};
const headers = { 'content-type': 'application/json'}
const body = JSON.stringify(data);
return this.httpClient.post(endPointURL,body,{'headers':headers})
在 endPointURL 上,我可以看到正文详细信息为空。但是如果我像这样删除标题:
return this.httpClient.post(endPointURL,body)
我可以正确地看到身体细节。我需要实现正文数据和标题。你能告诉我我做错了什么吗?
解决方法
将 headers
创建为 -
let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
// OR,let headers = new HttpHeaders().set('Content-Type','application/json');
并将其作为 {headers: headers}
传递。
此外,您不需要JSON.stringify
data
。
尝试类似 -
const data = {
'username': username,'password': password
};
let headers = new HttpHeaders({
'Content-Type': 'application/json'
});
return this.httpClient.post(endPointURL,data,{headers: headers});
,
按照步骤操作。
-
import { HttpClient,HttpHeaders } from "@angular/common/http";
-
const headers = { headers: new HttpHeaders({"Content-Type":"application/json"})} const body= { 'username': username,'password': password};
-
return this.httpClient.post(endPointURL,body,headers)