axios添加自定义headers时异常问题
#main.js中axios引入及定义
import axios from 'axios'
axios.defaults.baseURL = 'https://localhost:5001/api/'
Vue.prototype.$http = axios
#get请求调用
##请求参数中未添加data
###调用代码
async getData() {
var endCreatedDate = new Date()
var startCreatedDate = new Date()
startCreatedDate.setDate(startCreatedDate.getDate() - 2)
var res = await this.$http.get('/Crosstest', {
params: {
BarTestingStatus: 'TESTING_BAR_STATUS_HANDOVER',
BarStartCreatedTime: this.$formatDate('yyyy-MM-dd hh:mm:ss', startCreatedDate),
BarEndCreatedTime: this.$formatDate('yyyy-MM-dd hh:mm:ss', endCreatedDate)
},
headers: {
'Content-type': 'application/trolleyRFIDData+json'
}
})
console.log(res)
}
###打印出来的数据
" class="reference-link">
此时可看到headers里面并没有自定义字段“Content-type”,然后data是undefined。
##请求参数中添加data
###调用代码
async getData() {
var endCreatedDate = new Date()
var startCreatedDate = new Date()
startCreatedDate.setDate(startCreatedDate.getDate() - 2)
var res = await this.$http.get('/Crosstest', {
params: {
BarTestingStatus: 'TESTING_BAR_STATUS_HANDOVER',
BarStartCreatedTime: this.$formatDate('yyyy-MM-dd hh:mm:ss', startCreatedDate),
BarEndCreatedTime: this.$formatDate('yyyy-MM-dd hh:mm:ss', endCreatedDate)
},
headers: {
'Content-type': 'application/trolleyRFIDData+json'
},
data: null
})
console.log(res)
}
###打印出来的数据
此时可看到headers里面已包含自定义字段“Content-type”
axios版本是 0.26.1
查询各种资料也没有发现存在这样的问题,先做记录,后续再跟进。
2022-6-29 20:30:55–对于axios来说’Content-Type’是特殊字段?当将中间的’-‘去掉时就可正常添加到请求头了
headers: {
// eslint-disable-next-line quote-props
'ContentType': 'application/trolleyRFIDData+json'
}
推荐文章: