vue使用axios请求发生301 Moved Permanently错误

image.png

vue.config.js代码

devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000', //后端地址
        ws: true, //是否代理websockets
        changeOrigin: true,
        pathRewrite: {
          '^/api': ''
        }
      }
    }
  }

main.js代码

import axios from 'axios'
Vue.prototype.$axios = axios  
axios.defaults.baseURL = '/api'

import qs from "qs"
axios.interceptors.request.use(function (config) {
  if(config.method=="post"){
      config.data = qs.stringify(config.data);
  }
  return config;
}, function (error) {
  return Promise.reject(error);
})

请求代码

const data = {
      uid: 32953014,
    }
    this.$axios
      .post('/likelist', {
        data
      })
      .then(function (response) {
        console.info(response.data)
      })
      .catch(function (error) {
        console.info(error)
      })
阅读 8k
1 个回答
新手上路,请多包涵

亲测可用:
你看看你这个项目其他请求后端接口的地方 是不是url后面都跟着一个斜杠?
如果是的话 就好解决了 请求地址最后加个斜杠就好了
this.$axios.post('/likelist/', { data })
如上就行了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题