轉(zhuǎn)自:https://blog.csdn.net/doulvme/article/details/80816872
一、axios是Vue官方推薦的一個(gè)ajax插件,用于請(qǐng)求后的傳遞過(guò)來(lái)的數(shù)據(jù)兢仰。
1、安裝方式:npm install axios -S
2、在所需要使用axios的文件中引入axios, import axios from 'axios'
3、通過(guò)axios請(qǐng)求后端數(shù)據(jù)这弧,具體代碼如下:
axios.get('http://hn.algolia.com/api/v1/search_by_date',{
params:{//有關(guān)參數(shù)
tags:this.tag,
page:this.list.length/20+1
}
}).then((res)=>{//請(qǐng)求結(jié)果
console.log(res.data)
})
二、jsonp可用于跨域請(qǐng)求json數(shù)據(jù)虚汛。
1匾浪、安裝方式:npm install jsonp --save
2、在所需要使用jsonp的文件中引入jsonp, const jsonp=require('jsonp')
3泽疆、通過(guò)jsonp請(qǐng)求后端數(shù)據(jù)户矢,具體代碼如下:
jsonp(url,null,(err,data)=>{
if(err){
console.log(err)
}else{
console.log(data)
}
})