gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:06:52 27 4
gpt4 key购买 nike

我使用 angularjs $http 发送请求,对于基本授权,我提供了带有 'Authorization' 的 header : 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);

}

我在控制台中看到的是

not equal

Basic SOME_BASE64_ENCODED_STRING string 82

Basic SOME_BASE64_ENCODED_STRING string 82

为什么字符串不相等,为什么当我使用 btoa() 时我的 http 请求也不起作用,而当我向它提供 this.myString 时它起作用

最佳答案

更改:

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

致:

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

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

关于JavaScript - 比较两个相同的字符串但它们不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639696/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com