JavaScript - 比较两个相同的字符串,但它们不相等

时间:2015-06-04 09:11:51

标签: javascript angularjs

我使用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时,它可以正常工作

1 个答案:

答案 0 :(得分:2)

变化:

this.basicAuthorization = 'Basic'+' '+ btoa('username'+';'+'password');

要:

this.basicAuthorization = 'Basic'+' '+ btoa('username'+':'+'password');

在基本身份验证中,用户名和密码必须由:分隔。 See the HTTP Authentication RFC