我一直在尝试以某种方式存储表单数据,其中存在相同的键,但不同的值之间用,(逗号)分隔。我可以通过POST发送单个“键/值”对的表单数据,但不能发送“键/多个(值)”对的表单数据。

请共享您的输入。


 async handleSubmit(event) {
        event.preventDefault();
        const token = localStorage.getItem("token");
        let url = "https://api/slef_domain/assign_dId/?";
        var form = new FormData();
        form.append("sourcedomainID", this.state.sourceDomID);
        form.append("destInfra", this.state.destInID);
        form.append("destdomainID", this.state.destDomID);
        form.append("domainRangeId", this.state.domainRanID);

        // Figure out the way to append the data in form : SOMETHING LIKE BELOW DEPENDING UPON
        // THE TIMES USER ADDED THOSE ROWS BEFORE SUBMITTING THE FORM
        // form.append("sourcedomainID", this.state.sourceDomID);
        // form.append("destInfra", this.state.destInID);
        // form.append("destdomainID", this.state.destDomID);

        // Here we use the FETCH method to POST data and display the response back on Web.
        await fetch(url, {
            method: "POST",
            body: form,
            headers: { "Authorization": `Token ${token}` },
            "mimeType": "multipart/form-data",
        }).then((results) => {
            return results
        }).then(response => {
            console.log("Actual Response: ", response)
            if (response.status === 204) {
                console.log("Response 204: ", response)
                this.setState({ alertMessage: "Success" })
            }
            else {
                console.log("Response no code: ", response)
                this.setState({ alertMessage: "Unavailable" })
            }
        });
    }

最佳答案

如果您在服务器上使用PHP(根据MDN的example),则可以使用以下任一方法:

form.append('arr[]', 'value1');
form.append('arr[]', 'value2');


或仅使用:

var arr = ['value1', 'value2'];

form.append('arr', JSON.stringify(arr));



然后JSON.parse检索您的数组

09-20 01:21