react ant新开一个窗口并在往该窗口请求头中传递basic auth
在React中,可以使用window.open()方法来打开一个新的窗口,并使用fetch或XMLHttpRequest来发送请求并在请求头中传递basic auth。
以下是一个示例代码:
// 打开新窗口
const newWindow = window.open();
// 构建basic auth的base64编码字符串
const username = 'yourUsername';
const password = 'yourPassword';
const basicAuth = btoa(`${username}:${password}`);
// 发送请求并在请求头中传递basic auth
fetch('http://example.com/api', {
headers: {
Authorization: `Basic ${basicAuth}`,
},
})
.then(response => response.json())
.then(data => {
// 在新窗口中显示返回的数据
newWindow.document.write(JSON.stringify(data));
})
.catch(error => {
console.error(error);
});
请注意,这只是一个简单示例,并且没有包含错误处理。在实际应用中,请根据需要进行适当的错误处理和验证。
原文地址: http://www.cveoy.top/t/topic/i7m9 著作权归作者所有。请勿转载和采集!