简体   繁体   English

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

[英]JavaScript - comparing two same strings but they are not equal

I use angularjs $http to send a request, for basic authorization I provided headers with 'Authorization': this.basicAuthorization 我使用angularjs $http发送请求,对于基本授权,我在标头中提供了'Authorization': this.basicAuthorization

But it doesn't work, I found out that there is a problem in this.basicAuthorization . 但这不起作用,我发现this.basicAuthorization中存在问题。 When I replace it with a string 'Basic SOME_BASE64_ENCODED_STRING' 当我将其替换为字符串'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);

}

And what I see in a console is 我在控制台中看到的是

not equal 不相等

Basic SOME_BASE64_ENCODED_STRING string 82 基本的SOME_BASE64_ENCODED_STRING字符串82

Basic SOME_BASE64_ENCODED_STRING string 82 基本的SOME_BASE64_ENCODED_STRING字符串82

Why are the strings are not equal and why when I use btoa() also my http request is not working and when I provide it with this.myString it works 为什么字符串不相等,为什么当我使用btoa()时我的http请求也无法正常工作,而当我向它提供this.myString时, this.myString可以工作

Change: 更改:

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

To: 至:

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

In basic authentication the username and password must be seperated by : . 在基本身份验证中,用户名和密码必须用分隔: See the HTTP Authentication RFC . 请参阅HTTP身份验证RFC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM