標籤:web php host com www 寫法 data es2017 stat
在網上看到人家寫的vue-resource.js的get方法例子,
new Vue({
el:‘body‘,
data:{
},
methods:{
get:function(){
this.$http.get(‘get.php‘,{
a:1,
b:2
}).then(function(res){
alert(res.data);
},function(res){
alert(res.status);
});
}
}
});
開始的時候後台無論怎樣都擷取不到參數,原來正確get寫法如下:
this.$http.get(‘http://localhost:8393/Home/GetUsers‘,{
params: {
uname1: this.uname
}
})
.then(function (res) {
//賦值給alllist數組,
console.log(res.data);
this.adminUsers = res.data;
})
原來參數是要加入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},否則後台是無法擷取你傳遞的參數的
vue-resource.js的get和post的正確用法