Luk*_*own 2 javascript authentication authorization http angular
我一直在尝试多种方法来访问使用基本身份验证的API.问题是Angular 2无法输入用户名和密码.
如果您导航到基本的auth网站,它会要求您输入用户名和密码,然后在大多数浏览器上单击Log In或Cancel.如果你点击这个网站上的取消,它将返回一个字符串说明User pressed Cancel,而这正是我通过Angular的HTTP请求做到的.
在这个阶段,Authorization标头中的编码用户名和密码是否正确无关紧要,我只需要能够发送用户名和密码并让它尝试登录.
有什么建议?
request() {
let headers = new Headers();
headers.append('Authorization', "Basic dWRpZDM6cGFzc3dvcmQ=");
this.http.get('http://localhost:8888/api/', {
headers: headers
})
.subscribe(
data => this.example = data.text(),
err => this.logError(err.text()),
() => console.log('Request Complete')
);
console.log(this.example);
}
Run Code Online (Sandbox Code Playgroud)
使用用户名和密码添加授权应该有帮助.请尝试以下方法:
let username : string = 'username';
let password : string = 'password';
let headers = new Headers();
headers.append("Authorization", "Basic " + btoa(username + ":" + password));
headers.append("Content-Type", "application/x-www-form-urlencoded");
this.http.get('http://localhost:8888/api/', {
headers: headers
}).subscribe(
data => this.example = data.text(),
err => this.logError(err.text()),
() => console.log('Request Complete')
);
console.log(this.example);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
8928 次 |
| 最近记录: |