如何向 FormData 追加嵌套对象

发布于 2025-01-15 10:03:13 字数 468 浏览 3 评论 0原文

如何使用表单附加来发布此数组:

data = [
    {
        "title":"title 1",
        "subTitle": "subTitle 1",
        "productId": "[1,2,3]"
    },
    {
        "title":"title 2",
        "subTitle": "subTitle 1",
        "productId": "[4,5,6]"
    },
    {
        "title":"title 3",
        "subTitle": "subTitle 1",
        "productId": "[7,8,9]"
    }
]

如果我按如下方式提交数据: formData.append("data", JSON.stringify(this.data); 我无法仅获取值productId 中的id 键

how can post this array with form append:

data = [
    {
        "title":"title 1",
        "subTitle": "subTitle 1",
        "productId": "[1,2,3]"
    },
    {
        "title":"title 2",
        "subTitle": "subTitle 1",
        "productId": "[4,5,6]"
    },
    {
        "title":"title 3",
        "subTitle": "subTitle 1",
        "productId": "[7,8,9]"
    }
]

If I submit data as here : formData.append("data", JSON.stringify(this.data);
I cannot get just id key in value productId

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2025-01-22 10:03:13

您可以尝试这个

data.forEach(item => {
            formData.append(`data[]`, JSON.stringify(item));
        });

- 在后端,您需要解析它以发送到前端。

You can try this-

data.forEach(item => {
            formData.append(`data[]`, JSON.stringify(item));
        });

and in the backend, you need to parse it for sending to the front end.

农村范ル 2025-01-22 10:03:13

一旦你的数组被“stringify”,你需要使用 JSON.parse 来访问单个键

const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = obj.name;
</script>

同时查看你的数组,你将必须使用数组键这样做

obj[0].productId
obj[1].productId
obj[2].productId
obj[3].productId

,然后从那时提取数组

 for (let i  = 0 ; i <  obj[3].productId.length ; i++)
{ 
 console.log(obj[3].productId[i]);

 }

如果你需要,请告诉我有关它的更多信息

Once your array has been "stringify" you need to use JSON.parse to access individual key

const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}');
<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = obj.name;
</script>

Also looking at your array it you will have to do it like this with the array key

obj[0].productId
obj[1].productId
obj[2].productId
obj[3].productId

and from that point extract the array

 for (let i  = 0 ; i <  obj[3].productId.length ; i++)
{ 
 console.log(obj[3].productId[i]);

 }

Let me know if you need more info on it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文