在“;数据“;服务器端的标签-引导ajax调用

pass array in the "data" tag at server side - bootstrap ajax call

本文关键字:引导 ajax 调用 数据 服务器端 标签      更新时间:2023-09-26

我无法确定是否可以在数据标记中发送数组:我的客户端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);       

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

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

在服务器上,按","拆分阵列。

可以。首先使用方法,而不是类型帖子。像这样。。。

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.stringfy将对象/数组转换为字符串。

$.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);
    }
});