gpt4 book ai didi

javascript - 如何将数组传递到 ajax 调用中?

转载 作者:行者123 更新时间:2023-11-28 14:36:12 25 4
gpt4 key购买 nike

这是我当前的代码:

$.ajax({
type : 'POST',
url : 'url',
data : $('#form').serialize() + "&p=1"
}

我还需要传递这个数组:

cars = ["Saab", "Volvo", "BMW"];

如何在该 ajax 调用中传递汽车

最佳答案

您应该能够使用JSON.stringify()

$('#form').serialize() + "&p=1&cars=" + JSON.stringify(cars)
<小时/>

根据该字符串的外观,可能还需要使用例如encodeURIComponent()

$('#form').serialize() + "&p=1&cars=" + encodeURIComponent(JSON.stringify(cars))
<小时/>

根据评论更新

JSON.stringify() 的逆向是 JSON.parse()

关于javascript - 如何将数组传递到 ajax 调用中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49751133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com