最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Axios发布到错误的URL

IT培训 admin 11浏览 0评论

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)
  }
}
``

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)
  }
}
``

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论