Ajax“Put”请求失败

时间:2018-03-21 17:19:59

标签: javascript ajax put form-data

我正在尝试在reactjs应用程序中发出ajax“PUT”请求。我正在尝试发送formdata。当我发送请求时,我收到一条错误消息“所以需要字段”。我已经在formdata中添加了这些字段,但我仍然收到此错误。

以下是我的后端发出请求所需的格式

{
    "name": "",
    "dob": null,
    "born_location": "",
    "current_location": "",
    "description": "",
    "father_name": "",
    "mother_name": "",
    "partner_name": "",
    "religion": "",
    "caste": "",
    "occupation": "",
    "education": "",
    "actor": {
        "awards": "",
        "active_from": null,
        "active_till": null
    },
    "politician": {
        "description": "",
        "political_party": "",
        "constituency": "",
        "positions": "",
        "created_by": null
    }
}

以下是我制作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))

这就是我发出“PUT”请求的方式

var settings = {
        "async": true,
        "crossDomain": true,
        "url": url,
        "type": "PUT",
        processData: false,
        contentType: false,
        "credentials": 'include',
        "headers": {
            Authorization: "Token " +token
        },

        "data": formData,


        success: (response, textStatus, jQxhr) => {
            //this.props.tokenAction(response.auth_token);
            console.log("face name addded")
            // this.props.openFileUpload();
            // that.props.uploadProgress();
        }
    }

    $.ajax(settings).done((response) => {

    });

这就是我得到的错误

enter image description here

我做错了什么?

0 个答案:

没有答案