vue-resource.js的get和post的正确用法

 在网上看到人家写的vue-resource.js的get方法例子,

 1 new Vue({
 2 el:'body',
 3 data:{
 4 
 5 },
 6 methods:{
 7 get:function(){
 8 this.$http.get('get.php',{
 9 a:1,
10 b:2
11 }).then(function(res){
12 alert(res.data);
13 },function(res){
14 alert(res.status);
15 });
16 }
17 }
18 });

 

 

开始的时候后台无论怎样都获取不到参数,原来正确get写法如下:

 1 this.$http.get('http://localhost:8393/Home/GetUsers',{
 2 
 3 params: {
 4 uname1: this.uname
 5 }
 6 })
 7 .then(function (res) {
 8 //赋值给alllist数组,
 9 console.log(res.data);
10 this.adminUsers = res.data;
11 })

 

 

原来参数是要加入params:{字段1:'app',字段2:'pencil'}这个,跟踪后才能获得

 而post的正确参考如下:

this.$http.post('http://localhost:8393/Home/GetUsers',{
uname1: this.uname

}, { emulateJSON: true })
.then(function (res) {
//赋值给alllist数组,
console.log(res.data);
this.adminUsers = res.data;
})

如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项。启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type,就像普通的HTML表单一样。

说白了post在进行数据请求时;需要填写第三个参数{emulateJSON:true},否则后台是无法获取你传递的参数的

 

转载于:https://www.cnblogs.com/qkabcd/p/7407253.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值