【发布时间】:2019-08-18 06:29:48
【问题描述】:
我正在尝试使用 vue-axios 通过 https 进行发布请求。 但是,由于我使用的是我创建的自签名证书,因此出现以下错误:
net::ERR_CERT_AUTHORITY_INVALID
经过搜索,我发现大多数人通过以下方式解决了这个问题
const instance = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
});
instance.get('https://something.com/foo');
// At request level
const agent = new https.Agent({
rejectUnauthorized: false
});
axios.get('https://something.com/foo', { httpsAgent: agent });
我尝试了这两个选项,但都没有成功。 我为 https.Agent 使用了 npm https 模块。
有谁知道如何解决这个问题?还是我应该从 axios 更改为其他模块?
编辑:
我正在运行的代码段出现错误:
const axiosInstance = axios.create({
baseURL: 'https://localhost:5000',
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
});
axiosInstance.post('/user', LoginRequest,
{ headers: { 'Content-Type': 'application/json' } })
.then(response => this.assignLogin(response.data));
尝试更改为名为 needle 的模块并使用 https,但出现相同的错误:
针:
const headers = { 'Content-Type': 'application/json' };
const options = {
method: 'POST',
headers: headers,
rejectUnauthorized: false,
requestCert: true,
agent: false,
strictSSL: false,
}
needle.post('https://localhost:5000/user', LoginRequest, options).on('end', function() { })
https:
const options = {
hostname: 'localhost',
port: 5000,
path: '/user',
strictSSL: false,
rejectUnauthorized: false,
secureProtocol: 'TLSv1_method',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
this.assignLogin(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(LoginRequest);
req.end();
【问题讨论】:
-
你的代码在哪里?
-
在上面添加了axios片
-
如果您要通过所有这些麻烦来允许无效证书,为什么不在开发时以 http 模式运行您的服务器,然后在生产时获得与域匹配的有效证书?跨度>
-
在 axios 的文档中说:“
httpAgent和httpsAgent分别定义了在执行 http 和 https 请求时使用的自定义代理,在 node.js 中.”。我已经看到他们在文档中区分浏览器和节点,所以这个配置可能不适合浏览器(Vue)。至少它对我也不起作用。