Vue從了解到清晰-Vue2.0中使用vue-jsonp解決跨域問題(看看這篇就夠了)
1页衙、執(zhí)行命令:
npm install vue-jsonp --save
2、src/main.js中添加:
Vue.use(VueJsonp)
3旱幼、其它組件中基本使用方法:
this.$jsonp( rootApi+'/company/Info'', 傳參參數(shù)對(duì)象{} ).then(json => {
// 返回?cái)?shù)據(jù) json, 返回的數(shù)據(jù)就是json格式
}).catch(err => {
console.log(err)
})
4卵贱、其他實(shí)例:
this.$jsonp(ApiRoot+'/company/Info', {
callbackQuery: 'callbackParam', // jsonp key //請(qǐng)求傳遞參數(shù) 1
callbackName: 'jsonpCallback', // jsonp key //請(qǐng)求傳遞參數(shù) 2
name: 'zhangsan', // 請(qǐng)求傳遞參數(shù) 3
type: 6 // 請(qǐng)求傳遞參數(shù) 4
}
).then( json => {
//返回?cái)?shù)據(jù) json西乖, 返回的數(shù)據(jù)就是json格式
}). catch(err => {
console.log(err)
})
4.2:
this.$jsonp(ApiRoot+'/company/Info',
Object.assign(this.$store.state.common, {callbackQuery: 'callbackParam', callbackName: 'jsonpCallback'})
).then(json => {
_this.pageData= json.result
}).catch(err => {
console.log(err)
})