yiban 首先,你需要在项目中安装这个库:

npm install base-64

然后,你可以按照以下方式使用它:

import { encode } from 'base-64';

const username = 'myUsername';

const password = 'myPassword';

const credentials = `${username}:${password}`;

const base64Credentials = encode(credentials);

console.log(base64Credentials);

上述示例中,我们使用了 base-64 库中的 encode 函数来对字符串进行 Base64 编码。

const baseData = encode(username + ':' + password);

console.log(baseData, "这个是加密的");

const postData = {};

postData.authorization = baseData;

try {

const url = 'xxxxxxxx';

const response = await fetch(url, {

method: 'POST',

body: JSON.stringify(postData),

});

const result = await response.json();

// 处理返回的数据

console.log(result, "数据");

} catch (error) {

console.log(error);

}

一般用于加密处理。用法如上。

文章链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: