vue webpack-simple配合TP5.1本地环境开发之跨域访问

php代码,返回提交的数据

    public function message()
    {
        $data=Request::param();
        return json_encode($data);
    }

vue init webpack-simple

1、简单模板中,用axios的get方法,可以直接跨域访问。

mounted(){    //加载完成时
  console.log(this.$root.host);//获取主函数中的host属性,里面存放的域名
    this.axios.get(this.$root.host+'/index/message',{
      params:{
        id:'123',
        name:'456'
      }
      }).then(function(res){
        console.log(res);
      }).catch(function(error){
        console.log(error);
      });
    },

传值成功。

2、简单模板中,用axios的post方法,需要先在create中将数据进行转换才可以跨域访问。

        mounted(){
            console.log(this.$root.host);//域名在main.js中存放
            var server=this.axios.create({
                transformRequest: [//对数据转换成类似get传参的模式
                    data => {
                        data=qs.stringify(data);
                        console.log(data);
                        return data;
                    }
                ]
            })
            server.post(this.$root.host+'/index/message',{
                    id:'123',
                    name:'456'
            })
            .then(function(res){
                console.log(res);
            }).catch(function(error){
                console.log(error);
            });
        }

传值成功。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值