vue axios數(shù)據(jù)請求及vue中使用axios的方法
axios 簡介
axios 是一個基于Promise 用于瀏覽器和 nodejs 的 HTTP 客戶端,它本身具有以下特征:
--------------------------------------------------------------------------------
•從瀏覽器中創(chuàng)建 XMLHttpRequest
•從 node.js 發(fā)出 http 請求
•支持 Promise API
•攔截請求和響應(yīng)
•轉(zhuǎn)換請求和響應(yīng)數(shù)據(jù)
•取消請求
•自動轉(zhuǎn)換JSON數(shù)據(jù)
•客戶端支持防止 CSRF/XSRF
在vue中數(shù)據(jù)請求需要先安裝axios
npm i --save axios
我們在使用請求數(shù)據(jù)的頁面導(dǎo)入axios
import axios from "axios"
然后在methods里面寫數(shù)據(jù)的請求
methods:{
getInfo(){
let url = "url"
axios.get(url).then((res)=>{
//console.log(res)
this.list1 = res
})
}
在生命周期調(diào)用一下,一般我們數(shù)據(jù)請求使用的生命周期是Mounted
mounted() {
this.getInfo()
}
這樣我們就完成了axios的get方法請求
然后我們簡答的說一說post請求,post請求與get請求其實變得不多
postInfo() {
let url = "..."
var params = new URLSearchParams();
params.append('key', index);
axios.post(url, params).then((res) => {
console.log(res)
})
}
這樣我們就可以成功的使用post方法請求數(shù)據(jù)了
補充:下面看下vue中使用axios
1.安裝axios
npm:
$ npm install axios -S
cdn:
<script src=">
2.配置axios
在項目中新建api/index.js文件,用以配置axios
api/index.js
import axios from 'axios';
let http = axios.create({
baseURL: 'http://localhost:8080/',
withCredentials: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
},
transformRequest: [function (data) {
let newData = '';
for (let k in data) {
if (data.hasOwnProperty(k) === true) {
newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
}
}
return newData;
}]
});
function apiAxios(method, url, params, response) {
http({
method: method,
url: url,
data: method === 'POST' || method === 'PUT' ? params : null,
params: method === 'GET' || method === 'DELETE' ? params : null,
}).then(function (res) {
response(res);
}).catch(function (err) {
response(err);
})
}
export default {
get: function (url, params, response) {
return apiAxios('GET', url, params, response)
},
post: function (url, params, response) {
return apiAxios('POST', url, params, response)
},
put: function (url, params, response) {
return apiAxios('PUT', url, params, response)
},
delete: function (url, params, response) {
return apiAxios('DELETE', url, params, response)
}
}
這里的配置了POST、GET、PUT、DELETE方法。并且自動將JSON格式數(shù)據(jù)轉(zhuǎn)為URL拼接的方式
同時配置了跨域,不需要的話將withCredentials設(shè)置為false即可
并且設(shè)置了默認(rèn)頭部地址為:http://localhost:8080/,這樣調(diào)用的時候只需寫訪問方法即可
3.使用axios
注:PUT請求默認(rèn)會發(fā)送兩次請求,第一次預(yù)檢請求不含參數(shù),所以后端不能對PUT請求地址做參數(shù)限制
首先在main.js中引入方法
import Api from './api/index.js'; Vue.prototype.$api = Api;
然后在需要的地方調(diào)用即可
this.$api.post('user/login.do(地址)', {
"參數(shù)名": "參數(shù)值"
}, response => {
if (response.status >= 200 && response.status < 300) {
console.log(response.data);\\請求成功,response為成功信息參數(shù)
} else {
console.log(response.message);\\請求失敗,response為失敗信息
}
});
相關(guān)文章
Delete `,` 如何解決(vue eslint與prettier沖突)
這篇文章主要介紹了Delete `,` 如何解決(vue eslint與prettier沖突)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue項目中運用webpack動態(tài)配置打包多種環(huán)境域名的方法
本人分享一個vue項目里,根據(jù)命令行輸入不同的命令,打包出不同環(huán)境域名的方法。需要的朋友跟隨小編一起看看吧2019-06-06
Vue.js結(jié)合SortableJS實現(xiàn)樹形數(shù)據(jù)拖拽
本文主要介紹了Vue.js結(jié)合SortableJS實現(xiàn)樹形數(shù)據(jù)拖拽,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Vue.js中使用${}實現(xiàn)變量和字符串的拼接方式
這篇文章主要介紹了Vue.js中使用${}實現(xiàn)變量和字符串的拼接方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

