Vue通過axios發(fā)送ajax請求基礎(chǔ)演示
在Vue中是不支持發(fā)送ajax請求的,如果我們要在Vue中發(fā)送ajax請求,我們需借助第三方插件
常用發(fā)送ajax請求插件有兩個 vue-resource和axios,Vue.js 2.0 版本推薦使用 axios 來完成 ajax 請求。
axios項目源碼 https://github.com/axios/axios
axios引入方式
npm方式: npm install axios
bower方式 bower install axios
yarn方式 yarn add axios
CDN方式<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
axios基本使用
axios發(fā)送簡單get請求
//1.php
<?php
echo 123;
//html
<div id='app'>
<input type="button" @click='get' value='get' name="">
<input type="button" @click='post' value='post' name="">
<input type="button" @click='jsonp' value='jsonp' name="">
</div>
//js
<script type="text/javascript">
var vm = new Vue({
el:'#app',
methods:{
get(){
axios.get('1.php').then(res=>{
console.log(res);
})
}
}
})
</script>
then后面的匿名函數(shù)為請求成功的回調(diào)函數(shù)
打印結(jié)果如下

axios get傳參
1.直接寫在url后面
var vm = new Vue({
el:'#app',
methods:{
get(){
axios.get('1.php?id=1&name="測試"').then(res=>{
console.log(res);
})
}
}
})
2.通過params參數(shù)
var vm = new Vue({
el:'#app',
methods:{
get(){
axios.get('1.php',{params:{
id:1,
name:'測試'
}}).then(res=>{
console.log(res);
})
}
}
})
axios發(fā)送post請求
axios({
method: 'post',
url: '1.php',
params: {
firstName: 'Fred',
lastName: 'Flintstone'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
}).then(function(res){
console.log(res)
})
注意:直接使用axiox發(fā)送post請求時,會使后端接收不到數(shù)據(jù)
解決方法如下
一,在發(fā)送post請求時我們要手動設(shè)置請求頭
Content-Type:application/x-www-form-urlencoded并且我們將傳遞參數(shù)的屬性data換成了params,使用data發(fā)送數(shù)據(jù),后端接收不到
二,使用data發(fā)送數(shù)據(jù)時,我們可以在數(shù)據(jù)發(fā)送之前進行數(shù)據(jù)轉(zhuǎn)換轉(zhuǎn)換為key=value&key2=value2…的形式
axios({
url: '1.php',
method: 'post',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
},
//數(shù)據(jù)轉(zhuǎn)換
transformRequest:[data=>{
let res = ''
for (let item in data){
res +=item + "="+data[item]+"&";
}
return res;
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(res){
console.log(res)
})
以上就是Vue通過axios發(fā)送ajax請求基礎(chǔ)演示的詳細內(nèi)容,更多關(guān)于Vue通過axios發(fā)送ajax請求的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue + OpenLayers 快速入門學(xué)習(xí)教程
大家都知道使用 Openlayers可以很靈活自由的做出各種地圖和空間數(shù)據(jù)的展示。而且這個框架是完全免費和開源的,本文記錄下 Vue 使用 OpenLayers 入門,使用 OpenLayers 創(chuàng)建地圖組件的相關(guān)知識,需要的朋友一起學(xué)習(xí)下吧2021-09-09
vue項目動態(tài)渲染input,綁定的參數(shù)不實時更新問題
這篇文章主要介紹了vue項目動態(tài)渲染input,綁定的參數(shù)不實時更新問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue使用$store.commit() undefined報錯的解決
這篇文章主要介紹了vue使用$store.commit() undefined報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue3項目pc端瀏覽器樣式正常但移動端部分樣式失效問題的解決方法
這篇文章主要介紹了Vue3項目pc端瀏覽器樣式正常但移動端部分樣式失效問題的解決方法,文中通過圖文講解的非常詳細,具有一定的參考價值,需要的朋友可以參考下2024-07-07
vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法
這篇文章主要介紹了vue3+vite使用vite-plugin-svg-icons插件顯示本地svg圖標(biāo)的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
vue2實現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的代碼
這篇文章主要介紹了vue2實現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的代碼,需要的朋友可以參考下2018-08-08

