vue.js - Nuxt.js/Axios 自签名证书错误
问题描述
我在GET
从 http 到 https 发出本地请求时遇到问题。
情况:我使用一个叫Local( https://localwp.com/ )的工具来快速生成一个本地的WordPress开发环境。它的本地地址是https://backend.local(通过主机文件)。我还有一个 Nuxt.js 应用程序在http://localhost:3000上运行。
使用 axios,我尝试向GET
WordPress REST api 发出请求,但出现以下错误:
NuxtServerError
self signed certificate
如果我尝试该fetch
方法,我会得到以下响应:
FetchError
request to https://backend.local/wp-json/wp/v2/posts failed, reason: self signed certificate
它仅在我GET
向非本地 URL 发出请求时才有效。
但是经过几个小时的谷歌搜索并尝试了所有解决方案,没有任何效果,我现在有点沮丧:)
我的设置是:MacBook Pro 2019 Catalina 10.15.4,Chrome 83.0.4103.61
我希望有人能给我一个答案:)
谢谢!
解决方案
我按照这些步骤解决了这个问题:
1. 创建一个plugins
文件夹,如果它不存在。在plugins
文件夹中,使用以下内容创建axios.js
文件:
import https from 'https';
export default function ({ $axios }) {
$axios.defaults.httpsAgent = new https.Agent({ rejectUnauthorized: false });
}
2. 在nuxt.config.js
文件中,找到该plugins
部分,然后添加以下内容:
// Other configurations here
plugins: [
// Other plugins here
'@/plugins/axios',
],
// Rest of the file
推荐阅读
- python - 为什么 scipy.distance.cdist 在使用 float32(较慢)和 float64(较快)之间有很大的性能差异?
- java - 如何在 Wildlfy 11 上激活对 AsyncResponse (JAX-RS) 的支持
- elasticsearch - 从远程主机连接超时重新索引
- android - 以指定比例缩小图像大小 - android
- java - apache httpclient:收到致命警报:bad_certificate
- node.js - 如何通过 Heroku/Node.js 配置简单的静态 AWS S3 图像读取(403 访问错误)
- css - 对子主题所做的 CSS 更改未显示在网站中
- python - 验证列表不为空而没有 len
- vue.js - Vue2 - 从组件内的主实例调用方法
- python-3.x - 在 QPlainTextEdit (PyQt5) 中为 python 实现自动缩进