vue進(jìn)行post和get請求實(shí)例講解
使用axios:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
一、基本使用方法
1、get請求
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
?.then(function (response) {
? console.log(response);
?})
?.catch(function (error) {
? console.log(error);
?});
?
// Optionally the request above could also be done as
axios.get('/user', {
? params: {
? ?ID: 12345
? }
?})
?.then(function (response) {
? console.log(response);
?})
?.catch(function (error) {
? console.log(error);
?});2.Post請求
axios.post('/user', {
?firstName: 'Fred',
?lastName: 'Flintstone'
})
.then(function (response) {
?console.log(response);
})
.catch(function (error) {
?console.log(error);
});簡單示例:
// 在進(jìn)行 post 和 get 請求的時(shí)候,使用 axios 進(jìn)行訪問
// 進(jìn)行 get 請求
axios.get(url).then(function (response){
? ? console.log(response);
}).catch(function(error){
? ? console.log(error);
});
// 進(jìn)行post 請求 ? ? ? ? ? ?
axios.post(url,{firstName:'Fred',lastName:'Flintstone'}).then(function (response) {
? ? console.log(response);
}).catch(function (error) {
? ? console.log(error);
});這樣發(fā)送請求,雖然是可以發(fā)送,但是攜帶的參數(shù),是一個(gè)json字符串,會(huì)出現(xiàn)問題。所以我們在用post發(fā)送請求的時(shí)候,需要這樣:
axios({ ?
? ? method:'post', ?
? ? url:url, ?
? ? data:{title:this.title,content:this.content}, ?
? ? headers:{'Content-Type': 'application/x-www-form-urlencoded'}, ?
? ? transformRequest: function(obj) { ?
? ? ? ? var str = []; ?
? ? ? ? for(var p in obj){ ?
? ? ? ? ? ? str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); ?
? ? ? ? } ?
? ? ? ? return str.join("&"); ?
? ? } ?
}).then((res)=>{
? ? console.log(res.data);
});
上面這種只能提交一些簡單的數(shù)據(jù),對于復(fù)雜的數(shù)據(jù),可以考慮使用 QS 對數(shù)據(jù)進(jìn)行處理。
使用方法,如果找不到QS文件,可以只用 npm 安裝:
npm install qs

下載這個(gè)文件之后,使用 script 標(biāo)簽正常引入即可。
使用方法:
var formData = Qs.stringify({imgIds: [48,49],});
axios.post(url,Qs.stringify(this.formData)).then(function (response) {
? ? console.log(response);
}).catch(function (error) {
? ? console.log(error);
});或者是:
axios({
? ? method: 'post',
? ? url:url,
? ? data:Qs.stringify(this.formData),
}).then((res)=>{
? ? console.log(res);
});到此這篇關(guān)于vue進(jìn)行post和get請求實(shí)例講解的文章就介紹到這了,更多相關(guān)vue進(jìn)行post和get請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue結(jié)合路由配置遞歸實(shí)現(xiàn)菜單欄功能
這篇文章主要介紹了Vue結(jié)合路由配置遞歸實(shí)現(xiàn)菜單欄,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)火鍋工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
vue實(shí)現(xiàn)簡單實(shí)時(shí)匯率計(jì)算功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡單實(shí)時(shí)匯率計(jì)算功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
ElementUI的this.$notify.close()調(diào)用不起作用的解決
本文主要介紹了ElementUI的this.$notify.close()調(diào)用不起作用的解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Vue.extend實(shí)現(xiàn)掛載到實(shí)例上的方法
這篇文章主要介紹了Vue.extend實(shí)現(xiàn)掛載到實(shí)例上的方法,結(jié)合實(shí)例形式分析了Vue.extend實(shí)現(xiàn)掛載到實(shí)例上的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05
如何巧用Vue.extend繼承組件實(shí)現(xiàn)el-table雙擊可編輯(不使用v-if、v-else)
這篇文章主要給大家介紹了關(guān)于如何巧用Vue.extend繼承組件實(shí)現(xiàn)el-table雙擊可編輯的相關(guān)資料,不使用v-if、v-else,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

