javascript - Axios 发布到错误的 URL
问题描述
这是我在 CreatePage.vue 页面上使用的方法
methods: {
async createPost() {
try {
await PostService.createPost(this.form);
} catch (err) {
console.log(err);
}
}
}
};
这是 PostService 类
const axios = require('axios')
const url = 'api/post/'
class PostService {
static async createPost(post) {
return axios.post(url + 'create', post)
}
}
这是代理的 vue.config
const path = require('path')
module.exports = {
outputDir: path.resolve(__dirname, '../server/public'),
devServer: {
proxy: {
'/api': {
target: 'http://localhost:3000/',
}
}
},
}
当我发出一个应该转到 http://localhost:3000/api/post/create的发布请求时
问题在于它将当前页面地址附加到请求的开头,例如
http://localhost:3000/posts/api/post/create
(帖子页面,如果在仪表板页面上,将附加仪表板)
解决方案
问题出在您的 PostService 类中,因为您没有在 URL 中包含“/”,请将其更改为
const axios = require('axios')
const url = '/api/post/'
class PostService {
static async createPost(post) {
return axios.post(url + 'create', post)
}
}
``
推荐阅读
- java - 为什么我的脚本语言解析器不工作?
- paperjs - paperjs:一次拖动两个项目而不改变它们的相对位置
- ios - 如何解决两个框架具有相同类错误的问题?
- ios - 如何在 Swift 4 中为 UISlider 轨道添加边框颜色?
- javascript - Angular:仅在 ng build --prod 上构建 ES2015
- google-apps-script - 如何使用 Google Apps 脚本在一列中对多列进行分组?
- jenkins - 如何删除 Jenkinsfile 中的文件夹?
- ios - 在 TextView 中渲染新行
- scala - cat.ReaderT[F,A,B] 来自依赖项 A 的理解
- jboss - wildfly.service 的作业失败,因为控制进程以错误代码退出