vue-resource.js的get和post的正确用法
?在網(wǎng)上看到人家寫的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 });?
?
開始的時候后臺無論怎樣都獲取不到參數(shù),原來正確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數(shù)組, 9 console.log(res.data); 10 this.adminUsers = res.data; 11 })?
?
原來參數(shù)是要加入params:{字段1:'app',字段2:'pencil'}這個,跟蹤后才能獲得
?而post的正確參考如下:
this.$http.post('http://localhost:8393/Home/GetUsers',{
uname1: this.uname
}, { emulateJSON: true })
.then(function (res) {
//賦值給alllist數(shù)組,
console.log(res.data);
this.adminUsers = res.data;
})
如果Web服務(wù)器無法處理編碼為application/json的請求,你可以啟用emulateJSON選項。啟用該選項后,請求會以application/x-www-form-urlencoded作為MIME type,就像普通的HTML表單一樣。
說白了post在進行數(shù)據(jù)請求時;需要填寫第三個參數(shù){emulateJSON:true},否則后臺是無法獲取你傳遞的參數(shù)的
?
轉(zhuǎn)載于:https://www.cnblogs.com/qkabcd/p/7407253.html
總結(jié)
以上是生活随笔為你收集整理的vue-resource.js的get和post的正确用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python——LOL官方商城皮肤信息爬
- 下一篇: Android开源源码推荐(一)