vue.js之在vue项目开发时实现axios跨域访问,和post提交后台接收不到数据

一、开发时实现axios跨域访问
1.在config/index.js中添加下面代码

proxyTable: {
      '/': {
                  target: 'http://192.168.2.105:8888',//目标接口域名,有端口号记得加上端口号
                  changeOrigin: true,//是否跨域
                  pathRewrite: {
                      '^/': '/'//重写接口,后面可以使重写的新路径,一般不做更改
                  }
              }
    },

2.使用axios请求数据时

let url = '/process_post';
//使用axios请求数据
this.axios.post(url)
  .then(function (response) {
   //此处要使用self,如果使用this指向的是axios对象,不再是vue对象
    console.log(response)
   })
    .catch(function (error) {
        console.log(error);
    });

二、post提交后台接收不到数据
1.在src/main.js中添加

import qs from 'qs';

Vue.prototype.$qs = qs

2.使用axios请求数据时

      let url = '/process_post';
      let param = this.$qs.stringify({
        names:self.form.input1,
        passwords:self.form.input2
      })
      //使用axios请求数据
      this.axios.post(url,param)
          .then(function (response) {
              //此处要使用self,如果使用this指向的是axios对象,不再是vue对象
              console.log(response)
          })
          .catch(function (error) {
              console.log(error);
          });
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容