1.問題:axios跨域請求失敗,請求報錯或者返回數(shù)據(jù)為空(瀏覽器測試正常返回數(shù)據(jù))
原因:這次項目java后臺接受請求頭headers:{'Content-type':'application/x-www-form-urlencoded'},而axios默認(rèn)'Content-type':'application/json'
解決:
1、先設(shè)置headers
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
2、請求前參數(shù)處理
function _transformRequest(data) {
et ret = ''
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
return ret;
}
如圖

image.png