HTTP(端口 80)和 HTTPS(端口 443)是不同的协议,因此将 HTTPS XHR 请求发送到端口 80 可能会导致连接失败或安全漏洞。为了解决这个问题,我们需要在 Axios 请求中设置正确的协议和端口。
以下是使用 Axios 发送 HTTPS 请求并避免将它们发送到端口 80 的示例代码:
axios({
method: 'get',
url: 'https://example.com/api/data',
https: true,
port: 443,
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
在这里,我们将 https
参数设置为 true
,并将 port
参数设置为 443
,这是 HTTPS 默认使用的端口。这将确保 Axios 请求使用正确的协议和端口,并避免可能的安全问题。