最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟

 更新時(shí)間:2019年10月29日 11:07:10   作者:iimT  
今天小編就為大家分享一篇Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

Cover

在Vue中使用axios

這個(gè)老生常談了,還是先記錄一遍,方面后面自己查。

!!! 設(shè)置form-data請(qǐng)求格式直接翻到后面看。

1. 安裝axios

在項(xiàng)目下執(zhí)行npm install axios。

之后在main.js中,添加:

import axios from 'axios' //引入

//Vue.use(axios) axios不能用use 只能修改原型鏈 
Vue.prototype.$axios = axios

2. 發(fā)送GET請(qǐng)求

axios封裝了get方法,傳入請(qǐng)求地址和請(qǐng)求參數(shù),就可以了,同樣支持Promise。

//不帶參數(shù)的get請(qǐng)求

let url = "..."
this.$axios.get(url)
.then((res) => {
 console.log(res) //返回的數(shù)據(jù)
})
.catch((err) => {
 console.log(err) //錯(cuò)誤信息
})

不過它的參數(shù)需要寫在params屬性下,也就是:

//帶參數(shù)的get請(qǐng)求

let url = "...getById"
this.$axios.get(url, {
 params: {
  id: 1
 }
})
.then((res) => {
 console.log(res) //返回的數(shù)據(jù)
})
.catch((err) => {
 console.log(err) //錯(cuò)誤信息
})

2. 發(fā)送post請(qǐng)求

與上面相同,就是參數(shù)不需要寫在params屬性下了,即:

//帶參數(shù)的post請(qǐng)求

let url = "...getById"
let data = {
 id: 1
}

this.$axios.post(url, data)
.then((res) => {
 console.log(res) //返回的數(shù)據(jù)
})
.catch((err) => {
 console.log(err) //錯(cuò)誤信息
})

3. 經(jīng)典寫法

axios也可以用jQ的寫法,不過回調(diào)函數(shù)還是Promise的寫法,如:

this.$axios({
 method: 'post',
 url: '...',
 data: {
 firstName: 'Fred',
 lastName: 'Flintstone'
 }
}).then((res) => {
 console.log(res)
})

設(shè)置form-data請(qǐng)求格式

我用默認(rèn)的post方法發(fā)送數(shù)據(jù)的時(shí)候發(fā)現(xiàn)后端獲取不到數(shù)據(jù),然而在network中看到參數(shù)是的確傳出去的了。而且用postman測(cè)試的時(shí)候也是可以的,比較了下兩個(gè)的不同發(fā)現(xiàn)是postman使用的是form-data格式,于是用form-data格式再次請(qǐng)求,發(fā)現(xiàn)OJBK

在查找設(shè)置請(qǐng)求格式的時(shí)候花了點(diǎn)時(shí)間,網(wǎng)上的方案有好幾個(gè),這個(gè)我親測(cè)成功,發(fā)上來。

import axios from "axios" //引入

//設(shè)置axios為form-data
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.transformRequest = [function (data) {
 let ret = ''
 for (let it in data) {
  ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
 }
 return ret
}]


//然后再修改原型鏈
Vue.prototype.$axios = axios

以上這篇Vue 設(shè)置axios請(qǐng)求格式為form-data的操作步驟就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

奈曼旗| 江永县| 钦州市| 乌兰察布市| 温宿县| 嘉定区| 景德镇市| 水富县| 齐齐哈尔市| 贵港市| 阿克苏市| 大埔县| 遂宁市| 皋兰县| 电白县| 青河县| 台北市| 股票| 正宁县| 司法| 博野县| 德清县| 邵武市| 宣武区| 道孚县| 赞皇县| 二手房| 昌图县| 江陵县| 香港 | 尼玛县| 台北县| 邵阳市| 舟曲县| 开鲁县| 康保县| 海门市| 信丰县| 泰和县| 屏边| 安顺市|