Vue向后端傳數(shù)據(jù)后端接收為null問題及解決
Vue向后端傳數(shù)據(jù)后端接收為null
由于axios默認發(fā)送數(shù)據(jù)時,數(shù)據(jù)格式是Request Payload,而并非我們常用的Form Data格式,后端數(shù)據(jù)就為null,所以在發(fā)送之前,需要使用qs模塊對其進行處理。
他們的格式
- Request Payload:http://localhost:8080/login?zh=123,pw=123
- Form Data:http://localhost:8080/login,{zh=“123”,pw=“123”}
安裝qs
npm install qs
mian.js中添加
import qs from 'qs' //引入qs Vue.prototype.$qs = qs
vue請求
axios.post('http://localhost:8080/manage/doctor/login.do',
this.$qs.stringify({
doctorName:this.form.username,
password:this.form.password,
// test:3,
})
)
.then(response=>{
console.log(response);
})
//獲取失敗
.catch(error=>{
console.log(error);
alert('網(wǎng)絡(luò)錯誤,不能訪問');
})
我的后端用的java,給你們看下效果圖吧:


Vue捕獲后端拋出異常
修改vue項目中 src/utils/request.js中 service.interceptors.response.use內(nèi)容
設(shè)置前

設(shè)置后


service.interceptors.response.use(
(response) => {
loadingInstance &&
setTimeout(function () {
loadingInstance.close()
}, 500)
const res = response.data
return res
},
(error) => {
loadingInstance &&
setTimeout(function () {
loadingInstance.close()
}, 500)
if (error && error.response) {
var { status, data } = error.response
if (status === 401 || status === 403) {
if (!loginInstance && whiteRoutes.indexOf(requestUrl) === -1) {
loginInstance = MessageBox.confirm('登錄超時請重新登錄', '重新登錄', {
confirmButtonText: '好的',
type: 'warning'
})
.then(() => {
loginInstance = null
store.dispatch('user/resetToken').then(() => {
location.reload()
})
})
.catch(() => {
loginInstance = null
})
}
} else {
if (data) {
Message({
message: data,
type: 'error',
duration: 5 * 1000
})
} else {
Message({
message: data.message || '服務(wù)器異常,請稍后再試或聯(lián)系管理員',
type: 'error',
duration: 5 * 1000
})
}
}
} else {
Message({
message: '服務(wù)器異常,請稍后再試或聯(lián)系管理員',
type: 'error',
duration: 5 * 1000
})
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue表單驗證?trigger:'blur'OR?trigger:'change&ap
利用?elementUI?實現(xiàn)表單元素校驗時,出現(xiàn)下拉框內(nèi)容選中后校驗不消失的異常校驗情形,這篇文章主要介紹了Vue表單驗證?trigger:‘blur‘?OR?trigger:‘change‘?區(qū)別,需要的朋友可以參考下2023-09-09
Vue項目報錯:Uncaught SyntaxError: Unexpected token <
這篇文章主要介紹了Vue項目報錯:Uncaught SyntaxError: Unexpected token <,在引入第三方依賴的 JS 文件時,遇到的一個問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
vue自適應(yīng)布局postcss-px2rem詳解
這篇文章主要介紹了vue自適應(yīng)布局(postcss-px2rem)的相關(guān)知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-05-05
在vue中使用回調(diào)函數(shù),this調(diào)用無效的解決
這篇文章主要介紹了在vue中使用回調(diào)函數(shù),this調(diào)用無效的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

