当前位置:   article > 正文

vue3+vite+axios:解决跨域问题_vue3.0 vite跨域代理axios无法替换主路径

vue3.0 vite跨域代理axios无法替换主路径

采用代理的方式

1.vite.config.js

  1. import { fileURLToPath, URL } from 'node:url'
  2. import { defineConfig } from 'vite'
  3. import vue from '@vitejs/plugin-vue'
  4. // https://vitejs.dev/config/
  5. export default defineConfig({
  6. plugins: [vue()],
  7. resolve: {
  8. alias: {
  9. '@': fileURLToPath(new URL('./src', import.meta.url))
  10. }
  11. },
  12. server:{
  13. proxy: {
  14. '/api': {
  15. target: "https://voice.baidu.com/api/newpneumonia",
  16. changeOrigin: true,
  17. rewrite: (path) => path.replace(/^\/api/, '')
  18. }
  19. }
  20. }
  21. })

target:要请求的网址

2.使用

如果只是使用当前的网址那么url就为:/api

  1. axios({
  2. url:'/api',
  3. method:'get'
  4. }).then((res)=>{
  5. console.log(res.data);
  6. })

 参考文章:vite+axios解决跨域问题-pudn.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/770510
推荐阅读
相关标签
  

闽ICP备14008679号