POST application/json 適用於傳遞多層的json


本來以為自己寫了那么多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" // 適用於文件上傳


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM