可以使用httpOptions
对象将HttpHeaders
对象传递给Angular的post
方法。在httpOptions
对象中,定义一个headers
属性并将HttpHeaders
对象赋值给它。然后将该httpOptions
对象作为第二个参数传递给post
方法。
示例代码:
import { HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
this.http.post(url, data, httpOptions)
.subscribe(response => console.log(response));
注意:此代码示例将Content-Type
标头设置为application/json
,您可以将其更改为您需要的任何内容。