Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

使用axios报错 Request failed with status code 404

api

import {
  request
} from '@/utlis/request'

// banner
export function getBanner() {
  let res = request('/banner', 'GET')
  return res
}

axios

import axios from 'axios'
import {
  Toast
} from 'mint-ui'

export const instance = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 20 * 1000
})

// request
instance.interceptors.request.use(config => {
  return config
}, error => {
  Promise.reject(error)
})

// response
instance.interceptors.response.use(response => {
  const res = response.data
  return Promise.resolve(res)
}, error => {
  Toast(error.message)
  return Promise.reject(error)
})

export const request = async (url = '', type = 'GET', data = {}, isForm = false) => {
  let result
  type = type.toUpperCase()
  let requestOptions = {
    method: type,
    url: url
  }
  if (isForm) {
    let form = new FormData()
    Object.keys(data).forEach(key => {
      let value = data[key]
      if (Array.isArray(value)) {
        value.forEach(item => {
          form.append(key, item)
        })
      } else {
        form.append(key, data[key])
      }
    })
    data = form
  }
  requestOptions['headers'] = {
    'Content-type': isForm ? 'multipart/form-data' : 'application/json'
  }
  if (type === 'GET') {
    requestOptions['params'] = data
  } else {
    requestOptions['data'] = data
  }
  await instance(requestOptions).then(res => {
    result = res
  })
  return result
}

page

<template>
  <div class="index">
    <!-- banner -->
    <music-banner :json='bannerJson'></music-banner>
  </div>
</template>

<script>
import MusicBanner from '@/components/MusicBanner'
import {getBanner} from '@/api/index'
export default {
  name: 'index',
   components: { MusicBanner },

  data() {
    return{
      bannerJson: []
    }
  },

  mounted() {
    this.getBannerData()
  },

  methods: {
    getBannerData(){
      getBanner().then(res => {
        the.bannerJson = res.data
      })
    }
  }

}
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

404就是你请求的实际接口地址在后端找不到。
需要提供很多信息才能判断具体原因,比如是本地开发还是打包部署后的、你的process.env.VUE_APP_BASE_API值是什么、本地代理怎么配置的、后端完整的接口地址是什么


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...