本來以為自己寫了那么多post請求,ajax已經難不住了呢,
結果現實無比的殘酷,
后台換成了java,發多層級的json,后台就取不到了,
雖然到最后還是配置正確了,。。記錄下來,引以為戒,
axios.post("POST", "/URL", this.state.datas, {headers: {"Content-Type": "application/json"}})
.then( res => {
console.log(res.data)
})
.catch(err => {
console.log(err)
})
// 來一個原生版的
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var res = JSON.parse(xhr.responseText);
}
}
}
xhr.open("POST", "./toLogin.do", true);
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
xhr.send({})
其實重點就是請求頭信息
"Content-Type": "application/json"
// 順便一提
"Content-Type": "application/x-www-form-urlencoded" // 適用於大部分情況
"Content-Type": "multipart/form-data" // 適用於文件上傳