react ant新开一个窗口能够传递请求头
在React中使用window.open()方法可以打开一个新窗口。要在新窗口中传递请求头,可以使用URL参数来传递请求头信息。
以下是一个示例代码:
import React from 'react';
const NewWindow = () => {
const openNewWindow = () => {
const headers = {
Authorization: 'Bearer your-token',
'Content-Type': 'application/json',
};
const queryString = Object.keys(headers)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(headers[key])}`)
.join('&');
const url = `https://example.com/?${queryString}`;
window.open(url, '_blank');
};
return (
<button onClick={openNewWindow}>Open New Window</button>
);
};
export default NewWindow;
在上面的示例中,openNewWindow函数创建了一个包含请求头信息的headers对象。然后,使用Object.keys方法将请求头对象的键和值拼接成URL参数字符串。最后,拼接URL和URL参数,使用window.open()方法打开一个新窗口。
请注意,在生产环境中,应该避免在URL中直接传递敏感信息,如认证令牌。最好的做法是将敏感信息存储在服务器端,并在打开新窗口时使用安全的身份验证方法来获取这些信息。
原文地址: https://www.cveoy.top/t/topic/i73I 著作权归作者所有。请勿转载和采集!