vue webpack完整版配合TP5.1本地环境开发之跨域访问

先用简单模板测试完后,再用完整版的脚手架开发测试,发现一样适用。简单脚手架使用方法地址如下:

https://blog.csdn.net/lizheng8715/article/details/97032831

开始正题。

后台直接返回前台传递的参数,源码

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

1、get方法传参数,可以直接正常传参即可

            this.axios.get(http,{//http是后台地址
                params:{
                    'id':'12346',
                    'user':'name'
                }
            }).then(function(res){
                console.log(res);

            }).catch(function(error){
                console.log(error);
            });

vue webpack完整版配合TP5.1本地环境开发之跨域访问_第1张图片

传参成功。

2、post方法传递参数,需要先将参数用qs方法转换一下,qs方法在axios中自带

            var server=new this.axios.create({
                transformRequest: [//对数据转换成类似get传参的模式
                    data => {
                        data=qs.stringify(data);
                        console.log(data);
                        return data;
                    }
                ]
            })
            server.post(http,{
                // params:{
                    'id':'12346',
                    'user':'name'
                // }
            }).then(function(res){
                console.log(res);

            }).catch(function(error){
                console.log(error);
            });

vue webpack完整版配合TP5.1本地环境开发之跨域访问_第2张图片

你可能感兴趣的:(vue)