vue在進行動態數據交互時需要藉助vue-resource模塊中的$http。所以需要引用vue-resource.js和vue.js
語法:
get 請求:
語法: this.$http.get('url',{參數列表}).then(function(res){
處理請求成功的情況
res 是請求回來的數據,(。。。)
}, function(res){
處理請求失敗的情況
});
post 請求:
語法: this.$http.post('url',{參數列表},{emulateJSON:true}).then(function(res){
請求成功
}. function(res){
請求失敗
})
jsonp 請求
語法:this.$http.jsonp('url',{參數列表},{emulateJSON:true}).then(function(res){
處理請求成功
},function(res){
處理請求失敗
})
get請求一個純文本文件時:
this.$http.get(X.txt).then(
function(res){
console.log(res.data)
//請求成功的情況 res.data:需要的數據 res.status:狀態碼
},
function(res){
//請求失敗的情況 如果只傳入一個function時,即只處理請求成功的情況
console.log(res.tatus)
}
)
有參的get請求:
this.$http.get('接口',{ params:{ user:zhangsan,psd:123 } }).then(
function(res){//處理請求成功的回調函數},
function(res){//處理請求失敗的回調函數},
)
post請求:
this.$http.post('接口',{ user:zhangsan,psd:123 },{ emulateJSON: true }).then(
// 服務器不接收application/json的數據
function(res){//處理請求成功的回調函數},
function(res){//處理請求失敗的回調函數},
)
跨域請求jsonp :
this.$http.jsonp('域名',{ params:{ wd=11 } }).then(
function(res){//處理請求成功的回調函數},
function(res){//處理請求失敗的回調函數},
)