在使用axios攔截器時,返回500,報了"Required request body is missing: public"的錯誤,我的攔截器是這么寫的,參考了以下鏈接:
http://www.45fan.com/article.php?aid=1D2dBLoGSZ31XuGv#_label1
我這里的基礎地址在我本地換成了接口的域名地址。
import axios from 'axios'
export function request(config) {
// 1.創(chuàng)建axios的實例
const instance = axios.create({
// 設置基礎的url配置項,這樣接口處的url前面就不用寫了
baseURL: '基礎地址',
//設置請求超時時間
timeout: 5000
})
// 2.axios的攔截器,用不到的可以忽略這節(jié)
// 2.1.請求攔截的作用
instance.interceptors.request.use(config => {
return config
}, err => {
console.log('請求攔截err: '+err);
})
// 2.2.響應攔截
instance.interceptors.response.use(res => {
return res.data
}, err => {
console.log('響應攔截err: '+err);
})
// 3.發(fā)送真正的網(wǎng)絡請求
return instance(config)
}
然后在api/index.js的文件里面,這樣應用的:?
import {request} from '../utils/request'
//get請求
export function queryLogistics() {
return request({
url: '/接口地址',
method: 'post',
header: {
'Content-Type': 'application/json'
} // 已經(jīng)在request.js里面進行全局設置,也可以在請求里面局部設置其他headers
})
// Content-Type: application/json
// : 請求體中的數(shù)據(jù)會以json字符串的形式發(fā)送到后端,這種是axios默認的請求數(shù)據(jù)類型,我們只需將 參數(shù)序列化json字符串進行傳遞即可,無需多余的配置。
// Content-Type: application/x-www-form-urlencoded
// :請求體中的數(shù)據(jù)會以普通表單形式(鍵值對)發(fā)送到后端
// Content-Type: multipart/form-data
// : 它會將請求體的數(shù)據(jù)處理為一條消息,以標簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳文件。
}
然后報錯了, 加了header是因為搜索時候,發(fā)現(xiàn)很多朋友都是content-type沒確定才報的錯,但是我這里加上了之后依舊報錯了。之后發(fā)現(xiàn)是自己實在粗心,沒有把傳參需要的data加進去,這里真的很重要?。〖由现缶湍苷J褂昧?,不加header也可以了(我用的接口可以接受所有type的header)
export function queryLogistics(data) {
return request({
url: '/address',
method: 'post',
data,
// header: { // 已經(jīng)在request.js里面進行全局設置,也可以在請求里面局部設置其他headers
// 'Content-Type': 'application/json'
// }
})
?
注意點:
1. 要寫傳參
2. post對應的傳參是data, get對應的傳參是params
3. 仔細查看接口頭部需要的type是什么,一共有三種,application/json,application/x-www-form-urlencoded以及multipart/form-data文章來源:http://www.zghlxwxcb.cn/news/detail-692019.html
希望能夠幫到大家~文章來源地址http://www.zghlxwxcb.cn/news/detail-692019.html
到了這里,關于報錯 “Required request body is missing: public“ 的解決方案以及注意點(Vue, axios攔截器)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!