后端使用的是php:

1、不使用php自带的服务器启用服务(php think run)

2、axios请求的地址使用柚皮中的设置的服务地址,且在C:\Windows\System32\drivers\etc中的host文件设置服务地址:127.0.0.33  local.ztapi.com

3、在vite.config.js中添加设置跨域

server: {
    proxy: {
      "^/api": {
        target: "http://local.music.com",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, "")
      },
    },
  },

4、在axios封装的http.js请求中设置

const http = axios.create({
  baseURL: '/api',
  timeout: 6000,
})

5、在需要请求数据的地方调用

import http from '@/utils/http'
import { onMounted } from 'vue'

const getList = async () => {
  const res = await http.post('/api/archiveList')
  console.log('archiveList=', res)
}
onMounted( () => {
  getList()
})

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐