angular - 无法在 Angular http 请求标头中发送用户名和密码
问题描述
我需要在 HTTP 请求中将用户名和密码作为请求标头(不是查询参数)发送。
我get
在 Angular 服务中编写了以下方法。
http.get() {
let headers_object = new HttpHeaders();
headers_object.append('username', 'Test');
headers_object.append('password', 'Test123');
const httpOptions = {
headers: headers_object
};
const uri = 'http://localhost:8083/test';
let res;
this.http.get(uri, httpOptions)
.pipe(map(data => data)).subscribe(result => {
console.log(result);
res = result;
});
return res;
}
在这个方向上的任何帮助将不胜感激。
谢谢!
解决方案
由于HttpHeaders
是不可变的(下面不可变对象的定义:),
不可变对象是其状态在创建后无法修改的对象。-什么是可变和不可变数据结构?
这意味着您只能在同一实例中设置一次。
请参阅下面的示例:
let headers_object = new HttpHeaders()
.set('username', 'Test')
.set('password', 'Test123');
推荐阅读
- python-3.x - 如何将 RLE 中的输入更改为 Python 3 中的字节?
- firebase - Firebase 函数 (Google Cloud) 上的 Puppeteer PDF 创建超时
- ios - iOS 14 设备无法跟踪 Facebook 上的应用安装
- firefox - uBlock和代理socks5 firefox插件的兼容性问题
- python-3.x - 我很难使用 python opencv 将非正方形图像转换为正方形
- javascript - 从对象和键数组中查找值
- regex - hiveql 上的正则表达式:悬空元字符映射减少
- python - Python Django 在一个类别中安排帖子?
- jquery - 使用 Jquery UI 日期选择器突出显示特定日期
- sql-server - 如果数据库中存在数据,则 SSIS 失败