vue axios數(shù)據(jù)請(qǐng)求及vue中使用axios的方法
axios 簡(jiǎn)介
axios 是一個(gè)基于Promise 用于瀏覽器和 nodejs 的 HTTP 客戶端,它本身具有以下特征:
--------------------------------------------------------------------------------
•從瀏覽器中創(chuàng)建 XMLHttpRequest
•從 node.js 發(fā)出 http 請(qǐng)求
•支持 Promise API
•攔截請(qǐng)求和響應(yīng)
•轉(zhuǎn)換請(qǐng)求和響應(yīng)數(shù)據(jù)
•取消請(qǐng)求
•自動(dòng)轉(zhuǎn)換JSON數(shù)據(jù)
•客戶端支持防止 CSRF/XSRF
在vue中數(shù)據(jù)請(qǐng)求需要先安裝axios
npm i --save axios
我們?cè)谑褂谜?qǐng)求數(shù)據(jù)的頁(yè)面導(dǎo)入axios
import axios from "axios"
然后在methods里面寫數(shù)據(jù)的請(qǐng)求
methods:{
getInfo(){
let url = "url"
axios.get(url).then((res)=>{
//console.log(res)
this.list1 = res
})
}
在生命周期調(diào)用一下,一般我們數(shù)據(jù)請(qǐng)求使用的生命周期是Mounted
mounted() {
this.getInfo()
}
這樣我們就完成了axios的get方法請(qǐng)求
然后我們簡(jiǎn)答的說(shuō)一說(shuō)post請(qǐng)求,post請(qǐng)求與get請(qǐng)求其實(shí)變得不多
postInfo() {
let url = "..."
var params = new URLSearchParams();
params.append('key', index);
axios.post(url, params).then((res) => {
console.log(res)
})
}
這樣我們就可以成功的使用post方法請(qǐng)求數(shù)據(jù)了
補(bǔ)充:下面看下vue中使用axios
1.安裝axios
npm:
$ npm install axios -S
cdn:
<script src=">
2.配置axios
在項(xiàng)目中新建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方法。并且自動(dòng)將JSON格式數(shù)據(jù)轉(zhuǎn)為URL拼接的方式
同時(shí)配置了跨域,不需要的話將withCredentials設(shè)置為false即可
并且設(shè)置了默認(rèn)頭部地址為:http://localhost:8080/,這樣調(diào)用的時(shí)候只需寫訪問(wèn)方法即可
3.使用axios
注:PUT請(qǐng)求默認(rèn)會(huì)發(fā)送兩次請(qǐng)求,第一次預(yù)檢請(qǐng)求不含參數(shù),所以后端不能對(duì)PUT請(qǐng)求地址做參數(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);\\請(qǐng)求成功,response為成功信息參數(shù)
} else {
console.log(response.message);\\請(qǐng)求失敗,response為失敗信息
}
});
相關(guān)文章
基于Vue.js實(shí)現(xiàn)數(shù)字拼圖游戲
為了進(jìn)一步讓大家了解Vue.js的神奇魅力,了解Vue.js的一種以數(shù)據(jù)為驅(qū)動(dòng)的理念,本文主要利用Vue實(shí)現(xiàn)了一個(gè)數(shù)字拼圖游戲,其原理并不是很復(fù)雜,下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)。2016-08-08
Electron-vue開發(fā)的客戶端支付收款工具的實(shí)現(xiàn)
這篇文章主要介紹了Electron-vue開發(fā)的客戶端支付收款工具的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
vue構(gòu)建動(dòng)態(tài)表單的方法示例
這篇文章主要介紹了vue構(gòu)建動(dòng)態(tài)表單的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
VUEJS 2.0 子組件訪問(wèn)/調(diào)用父組件的實(shí)例
下面小編就為大家分享一篇VUEJS 2.0 子組件訪問(wèn)/調(diào)用父組件的實(shí)例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
undefined是否會(huì)變?yōu)閚ull原理解析
這篇文章主要為大家介紹了undefined是否會(huì)變?yōu)閚ull原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

