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" // 适用于文件上传
原文地址:https://www.cnblogs.com/daowangzhizhu-pt/p/6858925.html