我正在发出 ajax PUT 请求,并包含该请求的所有必填字段。我仍然收到 400 错误。
这是我的表单数据
var formData = new FormData()
var politician_id = this.props.image_reducer.politicianList[this.props.image_reducer.selectedPoliticianRow].person.id
console.log("id is "+ politician_id)
var politician = {
"description": document.getElementById('description-input').value,
"political_party": document.getElementById('party-input').value,
"constituency": document.getElementById('constituency-input').value,
"positions": document.getElementById('positions-input').value,
}
formData.append("name", document.getElementById('name-input').value)
formData.append("dob", document.getElementById('birth-input').value)
formData.append("born_location",document.getElementById("birth-location-input").value)
formData.append("current_location",document.getElementById('current-location-input').value)
formData.append("description", document.getElementById('description-input').value)
formData.append("father_name", document.getElementById('father-input').value)
formData.append("mother_name", document.getElementById('mother-input').value)
formData.append("partner_name", document.getElementById('name-input').value)
formData.append("religion", document.getElementById('religion-input').value)
formData.append("caste", document.getElementById('caste-input').value)
formData.append("occupation", "politician")
formData.append("education", document.getElementById('occupation-input').value)
formData.append("politician", JSON.stringify(politician))
这就是我提出请求的方式
var settings = {
"async": true,
"crossDomain": true,
"url": url,
"type": "PUT",
processData: false,
contentType: false,
"credentials": 'include',
"headers": {
Authorization: "Token " + token
},
"data": data,
success:( response, textStatus, jQxhr )=> {
console.log("info updated")
}
}
$.ajax(settings).done((response) => {
console.log("info updated success")
});
这是我遇到的错误
如果我离开“ Actor ”字段,从我的后端来看是完全可以的。请不要认为这是一个可能的答案。我也尝试包括“ Actor ”,但仍然遇到同样的错误。
我做错了什么?我是否以错误的方式制作表单数据?
更新
我发送的 JSON 是(这是来 self 打印表单数据的控制台)
最佳答案
看起来数据未在服务器端传递或未正确转换,因此验证失败。在请求 header 中,我可以注意到它正在发送 multipart/form-data
。
请更新 RequestMapping
的 consumes
以允许 multipart/form-data
即 consumes = { "multipart/form-数据”}
关于javascript - Ajax PUT 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49272948/