我在提交表單的時(shí)候,發(fā)現(xiàn)一直報(bào)500這個(gè)錯(cuò),說是超時(shí),但是我反復(fù)查看,并沒有什么問題。后來我請求大佬說是請求接口的參數(shù)的問題,使用對象JSON的形式就是獲取不到,必須是字符串拼接的格式才可以(默默地說,post默認(rèn)參數(shù)是對象JSON格式傳遞,傳字符串拼接的格式是后端改的,也就是說.....)
?
文章來源:http://www.zghlxwxcb.cn/news/detail-520753.html
?所以回歸正題,那就把post接口的參數(shù)轉(zhuǎn)換一下,使用qs.stringify()。我這里使用的是全局,在請求攔截器中判斷,如果是post請求,就轉(zhuǎn)換參數(shù)格式文章來源地址http://www.zghlxwxcb.cn/news/detail-520753.html
npm install qs
import qs from "qs";
service.interceptors.request.use(
(config) => {
// do something before request is sent
// 若是post請求,把參數(shù)是對象的格式轉(zhuǎn)換為字符串格式拼接
if (config.method == 'post') {
config.data = qs.stringify(config.data)
}
// 如果有token會(huì)得到當(dāng)前的token 并且直接添加到請求頭中, 以便后端得到當(dāng)前登錄的信息。
// if (store.getters.token) {
// // let each request carry token
// // ['X-Token'] is a custom headers key
// // please modify it according to the actual situation
// config.headers['token'] = getToken()
// }
return config;
},
(error) => {
// do something with request error
console.log(error); // for debug
return Promise.reject(error);
}
);
到了這里,關(guān)于【vue】post請求的參數(shù)格式轉(zhuǎn)換為字符串拼接的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!