我使用angularjs $http
发送请求,基本授权我提供的标题为'Authorization': this.basicAuthorization
但它没有用,我发现this.basicAuthorization
中存在问题。当我用字符串'Basic SOME_BASE64_ENCODED_STRING'
this.basicAuthorization = 'Basic'+' '+ btoa('username'+';'+'password');
this.myString = 'Basic SOME_BASE64_ENCODED_STRING';
console.log(this.basicAuthorization + " " + typeof this.basicAuthorization + " lenght " + this.basicAuthorization.length);
console.log(this.myString + " " + typeof this.myString + " length " + this.myString.length);
if(this.basicAuthorization == this.myString){
console.log("equal")
}
else{
console.log('not equal');
console.log(this.basicAuthorization + " " + typeof this.basicAuthorization + " lenght " + this.basicAuthorization.length);
console.log(this.myString + " " + typeof this.myString + " length " + this.myString.length);
}
我在控制台中看到的是
不等于
基本SOME_BASE64_ENCODED_STRING字符串82
基本SOME_BASE64_ENCODED_STRING字符串82
为什么字符串不相等?为什么当我使用btoa()时,我的http请求也无法正常工作?当我提供this.myString
时,它可以正常工作
答案 0 :(得分:2)
变化:
this.basicAuthorization = 'Basic'+' '+ btoa('username'+';'+'password');
要:
this.basicAuthorization = 'Basic'+' '+ btoa('username'+':'+'password');
在基本身份验证中,用户名和密码必须由:
分隔。 See the HTTP Authentication RFC