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
1.4k views
in Technique[技术] by (71.8m points)

vue-cli3 如何请求本地接口?

萌新求教大佬,vue脚手架利用axios如何请求本地接口?axios里的url要如何写?
project

node_modules
public
src
    api
        api.php
    assets
    components
    views
        send.vue
    App.vue
    main.js
vue.config.js

想在send.vue中用axios的post方法请求api文件夹中api.php文件,不知道这个url的路径改怎么写,萌新求教
顺便问一句,api的接口文件一般都放到哪个目录位置比较好


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

1 Reply

0 votes
by (71.8m points)

1.写完整路径, 让后端给你开cors, header("Access-Control-Allow-Origin: *");

2.写相对路径 + webpack 的反向代理,

vue.config.js :

proxy: {
  [VUE_APP_BASE_API]: {
    target: 'http://baidu.com', // 后端域名
    changeOrigin: true,
    pathRewrite: {
      '^/api': '' // URL正则匹配替换'^/api'替换为''
    }
  }
},

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

...