有一个接口,需要我多传Authorization (basic auth)的请求头,左思右想还是百度。
进入post函数查看方法是这样的,
发现框架已经帮我把headers写进去了
import { HttpHeaders } from '@angular/common/http';
// username 与 password 的值我没写出来
const headers = new HttpHeaders( {
Authorization: "Basic " + btoa(username + ':' + password)
});
// data是我需要传的json格式的参数,这里没写出来
this.http.post(environment, data, {}, {headers: headers}).subscribe((res: any) => {
//...
})
跨域可能是后台原因,相信其他头也可以这么传