我怕的window.open('/goods/category?data=' + encodeURIComponent(JSON.stringify(retData)), '_blank',)是这样的帮我改一下
时间: 2024-10-08 07:15:23 浏览: 47
如果你担心`window.open`可能导致安全风险或者编码问题,这里提供一种修改建议,我们将使用`fetch`或`axios`等现代的HTTP请求库来代替,同时确保数据序列化正确:
```javascript
// 使用fetch
const handleOpen = async () => {
const response = await fetch(`/goods/category`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(retData),
});
if (response.ok) {
// 如果响应成功,可以获取响应数据并跳转到新的页面
const url = await response.text(); // 或者 response.url()
window.location.href = url;
} else {
console.error('Failed to open:', response.statusText);
}
};
handleOpen();
// 或者使用axios
import axios from 'axios';
const handleOpen = async () => {
try {
const { data } = await axios.post(`/goods/category`, retData, {
headers: {'Content-Type': 'application/json'},
});
window.location.href = data.url; // 假设data对象有一个url属性
} catch (error) {
console.error('Error opening:', error.message);
}
};
handleOpen();
```
上述代码将数据发送到服务器,然后根据服务器响应决定是否跳转。这种方式既更安全也更符合RESTful设计原则。
阅读全文
相关推荐

















