我有需要发布作为参数到API端点的JSON数据。如何使用 HttpClient 在 Angular 中做到这一点?
export class resultService {
constructor(private http: HttpClient) {}
listResult() {
const options = {
"city": "value1",
"id": 12,
"c": null
};
//I need to call this API end point with POST method
//http://someapi.com/fetch?id={"city": "value1", "id": 12, "c": null}
return this.http.post('http://someapi.com/fetch', null, {
headers: new HttpHeaders({
'Content-Type': 'application/json'
}),
params: {"id": options}
//Options can't be passed here cz params doesn't accept JSON type
})
}
}
我是Angular的新手,所以这可能有些愚蠢。而且,我无法将此JSON作为正文发布,因为API端点仅接受它作为参数。非常感谢您提供有关适当博客/教程的帮助或指南。