0

我无法弄清楚是否可以在数据标签中发送一个数组:我的客户端 JS 代码如下所示:

                     $.ajax({
        url: '/mobiledoc/jsp/aco/Beneficiary/ptmmview.jsp',
        data: {
            "action":"savePatientRecords",
            "ptId":strPtId,
            "PatientVal":PatientVal,
            "Qid":Qid,
            "QType":QType
                            "Array" : ??
        },
        dataType: 'text',
        type: 'post',
        success: function (responseMsg) {
        // gets the response message back from server
            loadMilestoneData();
            alert(responseMsg);       
4

2 回答 2

0

服务器通常不会读取这样的数组。谨慎起见,先在客户端上展平阵列:

data: {
        ...
        "Array" : theArray.join(',')  // beware of values with ',' in them
      }

在服务器上,用“,”分割数组。

于 2013-07-17T16:03:17.260 回答
-2

是的你可以。首先使用方法而不是类型post。像这样...

method: 'post'

JQuery 应该为您序列化数据。

$.ajax({
    url: '/mobiledoc/jsp/aco/Beneficiary/ptmmview.jsp',
    data: {
        "action": "savePatientRecords",
            "ptId": strPtId,
            "PatientVal": PatientVal,
            "Qid": Qid,
            "QType": QType,
            "Array": [ 1, 2, 3 ]
    },
    dataType: 'text',
    method: 'post',
    success: function (responseMsg) {
        // gets the response message back from server
        loadMilestoneData();
        alert(responseMsg);
    }
});

如果没有,则使用JSON.stringify将您的对象/数组转换为字符串。

$.ajax({
    url: '/mobiledoc/jsp/aco/Beneficiary/ptmmview.jsp',
    data: JSON.stringify({
        "action": "savePatientRecords",
            "ptId": strPtId,
            "PatientVal": PatientVal,
            "Qid": Qid,
            "QType": QType,
            "Array": [ 1, 2, 3 ]
    }),
    dataType: 'text',
    method: 'post',
    success: function (responseMsg) {
        // gets the response message back from server
        loadMilestoneData();
        alert(responseMsg);
    }
});
于 2013-07-17T16:03:41.580 回答