javascript - Ajax PUT 请求失败

标签 javascript ajax html form-data

我正在发出 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")
        });

这是我遇到的错误

enter image description here enter image description here

如果我离开“ Actor ”字段,从我的后端来看是完全可以的。请不要认为这是一个可能的答案。我也尝试包括“ Actor ”,但仍然遇到同样的错误。

我做错了什么?我是否以错误的方式制作表单数据?

更新

我发送的 JSON 是(这是来 self 打印表单数据的控制台)

enter image description here

最佳答案

看起来数据未在服务器端传递或未正确转换,因此验证失败。在请求 header 中,我可以注意到它正在发送 multipart/form-data

请更新 RequestMappingconsumes 以允许 multipart/form-dataconsumes = { "multipart/form-数据”}

关于javascript - Ajax PUT 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49272948/

相关文章:

javascript - 在自定义 HTML 侧边栏中使用 .gs 文件中的变量值

php、jquery 和 ajax - 根据先前选择的值填充选择下拉列表

javascript - Ajax 安全性和代码隐藏功能

jquery - 如何使用 jQuery 知道 <table> 中 <tr> 的索引?

html - 以 HTML5 格式嵌入 Vimeo 视频的好方法是什么?

javascript - 列出包装 Shiny 传单的输入处理程序

javascript - 用 getElementById 填充数组

javascript - 猴子补丁 XMLHTTPRequest.onreadystatechange

html - Chrome 图像无法呈现

javascript - 如何手动选中 Material UI 复选框?