weixin_33711641 2018-03-14 08:35 采纳率: 0%
浏览 104

Ajax PUT请求失败

I am making an ajax PUT request and including all the required fields for the request. I am still getting 400 error.

This is my formdata

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))

This is how I am making request

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")
        });

This is the error I am getting

enter image description here enter image description here

It is perfectly ok from my backend side if I leave "actor" field. Please don't consider that to be a possible answer. I tried including "actor" also but still got same error.

What am I doing wrong? Am I making formdata in wrong manner?

Update

The JSON I am sending is (This is from console where I printed formdata)

enter image description here

  • 写回答

2条回答 默认 最新

  • weixin_33730836 2018-03-14 08:55
    关注

    Looks like the data is either not passed or not converted properly on the server side and hence the validation failure. In the request headers I can notice it is sending multipart/form-data.

    Do update the consumes of your RequestMapping to allow multipart/form-data i.e. consumes = { "multipart/form-data" }

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘