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

來源:互聯網
上載者:User

標籤: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的正確用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.