Axios发布到错误的URL

时间:2019-02-12 18:51:41

标签: javascript node.js vue.js axios

这是我在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 (帖子页面,如果在仪表盘页面上,则会附加仪表盘)

1 个答案:

答案 0 :(得分:0)

问题出在PostService类中,因为您未在URL中包含“ /”,请将其更改为

const axios = require('axios')
const url = '/api/post/'

class PostService {
  static async createPost(post) {
    return axios.post(url + 'create', post)
  }
}
``
相关问题