vue.js - Vue.js - vue.config.js 中的代理被忽略
问题描述
我一直在搜索和阅读有关此主题的文档,但我无法让它发挥作用。 https://cli.vuejs.org/config/#devserver-proxy
我通过命令正常地制作了我的 Vue.js 应用程序
vue create my-app
所以我通过命令运行应用程序
npm run serve
在http://localhost:8080/上。相当标准的东西。
但是我的应用程序需要一个在https://localhost/上运行的 PHP 后端, 所以我在根目录的 vue.confic.js 文件中设置了代理设置。
vue.confic.js 文件内容:
module.exports = {
devServer: {
proxy: {
'^/api': {
target: 'https://localhost/',
ws: true,
changeOrigin: true
}
}
}
};
我正在尝试让 axios 调用地址上的脚本
https://localhost/test.php
axios 调用是
mounted() {
this.$axios.get('api/test.php', {})
.then(response => { console.log(response.data) })
.catch(error => { console.log(error) });
},
但由于某种原因,我无法弄清楚我仍然得到
GET http://localhost:8080/api/test.php 404 (Not Found)
先感谢您。我会很高兴任何提示。
解决方案
您的 axios 调用将使用网页使用的任何协议向 API 发出请求。
该错误表明您正在进行 http 调用,但您的 webpack 配置只是欺骗 https。您是从发出请求的页面访问 https 吗?
您也可以尝试更新您的 webpack 代理服务器,使其看起来像这样
module.exports = {
//...
devServer: {
proxy: {
'/api': {
target: 'https://other-server.example.com',
secure: false
}
}
}
};
其他调试步骤:从终端卷曲您的 Api 端点以查看是否是协议问题
推荐阅读
- html - jQuery Mobile 内联文本输入和按钮
- android - Xamarin 应用程序无法从模拟器中获取 api 数据,在物理设备上运行良好
- java - PDFBox 没有将我想要的消息写入页面
- powershell - 如何使用 PowerShell 将文件夹从一个位置移动到另一个位置
- javascript - 如何在 JavaScript 中处理嵌套函数中的变量
- ruby-on-rails - Does “#includes” position matter on rails?
- mysql - Sqoop import query where ID between row_numbers does not work
- vbscript - 如何使用 PCOMM VBScript 提取正在运行的脚本名称
- javascript - 彼此内部有多个 for 循环
- javascript - 不使用lookbehind重写此正则表达式 - 带有 JS 的无效正则表达式