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);
});
}
传值成功。