如何在jquery中转换json格式的数组

How to convert array in json format in jquery

本文关键字:json 格式 数组 转换 jquery      更新时间:2023-09-26

我试图以json格式发送ids(作为数组),因为我使用JSON.stringify(ids);并存储在变量z中,我在服务器端使用数组来获取值。

var ids = $("#infolist li div.No").map(function() {
    return this.id;
}).get();
alert(ids);
console.log(ids);
if (ids) {
    var z = JSON.stringify(ids);
    alert(z);
    $.ajax({
        url: "UpdateNotification",
        dataType: "json",
        data: {ids: z},
        success: function(data) {
            console.log(z);
            alert("success " + data.st);
        }
    });
}

console.log(ids)显示["25","27","28"]console.log(z)显示["25","27","28"]

我的问题是它没有调用服务器端方法。我可能是由于不适当的json格式的数据。如何使它正确?

编辑

如果我直接使用

http://localhost: 8084/tt/UpdateNotification吗?id = % 5 b % 2225% 2 c % 22% 2 c % 2227% 22% 2228% 22% 5 d

没有调用服务器端方法。这是json . strategy (ids)的输出。

但是如果我使用

http://localhost: 8084/tt/UpdateNotification ? id = % 22和2225%;id = % 22和2226%;id = % 2227% 22

调用服务器端方法

不需要var z = JSON.stringify(ids);

只做:

// ...
data: { ids: ids },
// ...