vue發(fā)送ajax請(qǐng)求詳解
vue本身不支持發(fā)送AJAX請(qǐng)求,需要使用vue-resource(vue1.0版本)、axios(vue2.0版本)等插件實(shí)現(xiàn)
axios是一個(gè)基于Promise的HTTP請(qǐng)求客戶(hù)端,用來(lái)發(fā)送請(qǐng)求,也是vue2.0官方推薦的,同時(shí)不再對(duì)vue-resource進(jìn)行更新和維護(hù)
本文為大家介紹vue使用axios發(fā)送AJAX請(qǐng)求
首頁(yè)安裝并引入axios
1、npm install axios -S #直接下載axios組件,下載完畢后axios.js就存放在node_modules\axios\dist中
2、網(wǎng)上直接下載axios.min.js文件
3、通過(guò)script src的方式進(jìn)行文件的引入<script src="js/axios.min.js"></script>
axios基本使用方法
發(fā)送get請(qǐng)求
1、基本使用格式
格式1:axios([options]) #這種格式直接將所有數(shù)據(jù)寫(xiě)在options里,options其實(shí)是個(gè)字典
格式2:axios.get(url[,options]);
2、傳參方式:
通過(guò)url傳參
通過(guò)params選項(xiàng)傳參
下面我們來(lái)看一個(gè)vue發(fā)送ajax get請(qǐng)求實(shí)例代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>發(fā)送AJAX請(qǐng)求</title>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#itany',
data:{
user:{
name:'alice',
age:19
},
},
methods:{
send(){
axios({
method:'get',
url:'http://www.baidu.com?name=tom&age=23'
}).then(function(resp){
console.log(resp.data);
}).catch(resp => {
console.log('請(qǐng)求失?。?+resp.status+','+resp.statusText);
});
},
sendGet(){
axios.get('server.php',{
params:{
name:'alice',
age:19
}
})
.then(resp => {
console.log(resp.data);
}).catch(err => { //
console.log('請(qǐng)求失?。?+err.status+','+err.statusText);
});
},
}
});
}
</script>
</head>
<body>
<div id="itany">
<button @click="send">發(fā)送AJAX請(qǐng)求</button>
<button @click="sendGet">GET方式發(fā)送AJAX請(qǐng)求</button>
</div>
</body>
</html>
發(fā)送post請(qǐng)求(push,delete的非get方式的請(qǐng)求都一樣)
1、基本使用格式
格式:axios.post(url,data,[options]);
2、傳參方式
1、自己拼接為鍵值對(duì)
2、使用transformRequest,在請(qǐng)求發(fā)送前將請(qǐng)求數(shù)據(jù)進(jìn)行轉(zhuǎn)換
3、如果使用模塊化開(kāi)發(fā),可以使用qs模塊進(jìn)行轉(zhuǎn)換
4、注釋?zhuān)篴xios默認(rèn)發(fā)送post數(shù)據(jù)時(shí),數(shù)據(jù)格式是Request Payload,并非我們常用的Form Data格式,所以參數(shù)必須要以鍵值對(duì)形式傳遞,不能以json形式傳參
下面看是一個(gè)vue發(fā)送ajax post請(qǐng)求實(shí)例代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>發(fā)送AJAX請(qǐng)求</title>
<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script>
window.onload=function(){
new Vue({
el:'#itany',
data:{
user:{
name:'alice',
age:19
},
},
methods:{
sendPost(){
// axios.post('server.php',{
// name:'alice',
// age:19
// }) //該方式發(fā)送數(shù)據(jù)是一個(gè)Request Payload的數(shù)據(jù)格式,一般的數(shù)據(jù)格式是Form Data格式,所有發(fā)送不出去數(shù)據(jù)
// axios.post('server.php','name=alice&age=20&') //方式1通過(guò)字符串的方式發(fā)送數(shù)據(jù)
axios.post('server.php',this.user,{ //方式2通過(guò)transformRequest方法發(fā)送數(shù)據(jù),本質(zhì)還是將數(shù)據(jù)拼接成字符串
transformRequest:[
function(data){
let params='';
for(let index in data){
params+=index+'='+data[index]+'&';
}
return params;
}
]
})
.then(resp => {
console.log(resp.data);
}).catch(err => {
console.log('請(qǐng)求失敗:'+err.status+','+err.statusText);
});
},
}
});
}
</script>
</head>
<body>
<div id="itany">
<button @click="send">發(fā)送AJAX請(qǐng)求</button>
<button @click="sendGet">GET方式發(fā)送AJAX請(qǐng)求</button>
</div>
</body>
</html>
上面我們所介紹的vue發(fā)送ajax請(qǐng)求都是在同一域名下進(jìn)行的也就是同域或者說(shuō)是同源
那么vue如何發(fā)送跨域的ajax請(qǐng)求呢?
vue發(fā)送跨域ajax請(qǐng)求
1、須知:axios本身并不支持發(fā)送跨域的請(qǐng)求,沒(méi)有提供相應(yīng)的API,作者也暫沒(méi)計(jì)劃在axios添加支持發(fā)送跨域請(qǐng)求,所以只能使用第三方庫(kù)
2、使用vue-resource發(fā)送跨域請(qǐng)求
3、 安裝vue-resource并引入
npm info vue-resource #查看vue-resource 版本信息
cnpm install vue-resource -S #等同于cnpm install vue-resource -save
4、基本使用方法(使用this.$http發(fā)送請(qǐng)求)
this.$http.get(url, [options])
this.$http.head(url, [options])
this.$http.delete(url, [options])
this.$http.jsonp(url, [options])
this.$http.post(url, [body], [options])
this.$http.put(url, [body], [options])
this.$http.patch(url, [body], [options])
下面再來(lái)看兩個(gè)實(shí)例
向360搜索發(fā)送數(shù)據(jù)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>發(fā)送AJAX請(qǐng)求</title>
<script src="js/vue.js"></script>
<script src="js/axios.js"></script>
<script src="js/vue-resource.js"></script>
</head>
<body>
<div id="itany">
<a>{{name}}</a>
<button v-on:click="send">sendJSONP</button>
</div>
</body>
<script>
new Vue({
el: '#itany',
data:{
name: 'alice',
age: 19
},
methods:{
send:function(){
// https://sug.so.#/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=a
this.$http.jsonp('https://sug.so.#/suggest',
{params:{
word:'a'
}}
).then(function (resp) {
console.log(resp.data)
})
}
}
})
</script>
</html>
vue發(fā)送跨域ajax請(qǐng)求帶jsonp參數(shù)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>發(fā)送AJAX請(qǐng)求</title>
<script src="js/vue.js"></script>
<script src="js/axios.js"></script>
<script src="js/vue-resource.js"></script>
</head>
<body>
<div id="itany">
<button v-on:click="send">向百度搜索發(fā)送JSONP請(qǐng)求</button>
</div>
</body>
<script>
new Vue({
el:'#itany',
data:{
name:'za'
},
methods:{
send:function () {
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
{params:{wd:'a'},
jsonp:'cb', //百度使用的jsonp參數(shù)名為cb,所以需要修改,默認(rèn)使用的是callbakc參數(shù)就不用修改
}).then(function (resp) {
console.log(resp.data)
}).catch(function (err) {
console.log(err)
})
}
}
})
</script>
</html>
Vue作為一個(gè)沒(méi)有入侵性的框架并不限制你使用ajax框架
使用了Vue后,ajax部分你可以做如下選擇:
1.使用JS原生XHR接口
2.引入JQuery或者Zepto 使用$.ajax();
3.Vue的github上提供了vue-resource插件 :
4.使用 fetch.js
5.自己封裝一個(gè)ajax庫(kù)
至于哪種方式適合自己的項(xiàng)目大家可以自行選擇
相關(guān)文章
Elementui按鈕設(shè)置默認(rèn)選中狀態(tài)的實(shí)現(xiàn)過(guò)程
這篇文章主要給大家介紹了關(guān)于Elementui按鈕設(shè)置默認(rèn)選中狀態(tài)的實(shí)現(xiàn)過(guò)程,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Elementui具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07
vue實(shí)現(xiàn)大文件切片斷點(diǎn)續(xù)傳
上傳文件,基本上用了input框就可以解決,但大文件應(yīng)該怎樣上傳呢,一次性上傳明顯不現(xiàn)實(shí),因?yàn)槊看我粩嗑W(wǎng),那就會(huì)從頭開(kāi)始上傳,所以切片勢(shì)在必行,關(guān)鍵就是如何切,怎么保障文件數(shù)據(jù)不會(huì)丟失,同時(shí)優(yōu)化上傳保障機(jī)制,本文就來(lái)給大家講講如何上傳大文件2023-07-07
vscode中vue代碼提示與補(bǔ)全沒(méi)反應(yīng)解決(vetur問(wèn)題)
這篇文章主要給大家介紹了關(guān)于vscode中vue代碼提示與補(bǔ)全沒(méi)反應(yīng)解決(vetur問(wèn)題)的相關(guān)資料,文中通過(guò)圖文將解決的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
vue項(xiàng)目打包優(yōu)化方式(讓打包的js文件變小)
這篇文章主要介紹了vue項(xiàng)目打包優(yōu)化方式(讓打包的js文件變小),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的購(gòu)物車(chē)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
vue-cli項(xiàng)目代理proxyTable配置exclude的方法
今天小編就為大家分享一篇vue-cli項(xiàng)目代理proxyTable配置exclude的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue移動(dòng)端項(xiàng)目實(shí)現(xiàn)使用手機(jī)預(yù)覽調(diào)試操作
這篇文章主要介紹了Vue移動(dòng)端項(xiàng)目實(shí)現(xiàn)使用手機(jī)預(yù)覽調(diào)試操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
解決Vue在封裝了Axios后手動(dòng)刷新頁(yè)面攔截器無(wú)效的問(wèn)題
這篇文章主要介紹了解決VUE在封裝了Axios后手動(dòng)刷新頁(yè)面攔截器無(wú)效的問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11

