Vue向后端傳數(shù)據(jù)后端接收為null問題及解決
Vue向后端傳數(shù)據(jù)后端接收為null
由于axios默認(rèn)發(fā)送數(shù)據(jù)時,數(shù)據(jù)格式是Request Payload,而并非我們常用的Form Data格式,后端數(shù)據(jù)就為null,所以在發(fā)送之前,需要使用qs模塊對其進(jìn)行處理。
他們的格式
- 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組件中節(jié)流函數(shù)的失效的原因和解決方法
這篇文章主要介紹了vue組件中節(jié)流函數(shù)的失效和解決方法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-12-12
詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別
這篇文章主要介紹了詳談vue中router-link和傳統(tǒng)a鏈接的區(qū)別,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vue中對iframe實現(xiàn)keep alive無刷新的方法
這篇文章主要介紹了Vue中對iframe實現(xiàn)keep alive無刷新的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

