網(wǎng)絡請求
- 使用 vue-resource.js 庫
- 借助于 $http 完成
- 語法: 使用 promise 語法規(guī)范
- 常用的交互類型:get / post / jsonp
get類型
- 語法:
this.$http.get( 'url' , {
params:{ key1:val1 , key2:val2 ...}
})
.then(
function(res){處理請求成功的情況},
function(res){處理請求失敗的情況}
)
post類型
- 語法:
this.$http.post( 'url' , {
key1:val1 , key2:val2 ...
},{emulateJSON:true})
.then(
function(res){處理請求成功的情況},
function(res){處理請求失敗的情況}
)
{emulate:true} //模擬json數(shù)據(jù)格式,將參數(shù)傳遞過去
jsonp請求
- 語法:
this.$http.jsonp( 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su' , {
params:{ wd:'lol'},
jsonp='cb' //默認是callback,如跨域請求百度時cb
})
.then(
function(res){處理請求成功的情況},
function(res){處理請求失敗的情況}
)