0
点赞
收藏
分享

微信扫一扫

将js中的 array 传输到后端


前端JS代码:

var conditons = [];
var test1 = new Object();
test1.name="1";
test1.id="2";
var test2 = new Object();
test2.name="1";
test2.id="2";
conditons.push(test1);
conditons.push(test2);
$(function(){
  $.ajax({
    async:"false",
    type:'post',
    url:'链接', 
    data:{name:"123",conditions:JSON.stringify(conditons)},
    dataType : 'json', 
    success:function(data){
      console.log(data);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown){
      alert("error");
    }
  });
});

重点注意:将对象数组转为JSON形式的字符串:JSON.stringify

后端获取:

String conditions = request.getParameter("conditions");
JSONArray conditionList = JSONArray.fromObject(conditions);

   JSONArray outListarray = JSONArray.fromObject(outListstr);
              JSONArray inListarray = JSONArray.fromObject(inListstr);
               List inList = JSONArray.toList(inListarray);
               List outList = JSONArray.toList(outListarray); 

java 后端接收

JSONArray outListarray = JSONArray.fromObject(outListstr);
		       JSONArray inListarray = JSONArray.fromObject(inListstr);
		       ArrayList inList = (ArrayList) JSONArray.toList(inListarray);
		       ArrayList outList = (ArrayList) JSONArray.toList(outListarray);

js接收后台java 中List  转换为array()

if(customerChannelFlag) {
                            waf.doPost({
                                action: "targetterminal",
                                async: false,
                                data: {
                                    _method: "entryChange",
                                    cellnames: "SelectcustomerChannelFlag",
                                    customerChannelFlag: customerChannelFlag,
                                    customerId: customer.id,
                                    materialId: material.id,
                                    billID:_self.getCurrentModel().id,
                                    row:rowText,
                                    key:cellname,
                                    adminOrgID: waf("#basedataDept").wafPromptBox("getValue")==null?"": waf("#basedataDept").wafPromptBox("getValue").id,
                                    outList : JSON.stringify(outList),//输出参数
                                    inList : JSON.stringify(inList)   // 输入参数 
                                },
                                success: function (data) {
                                    console.log("判断目标终端库外,提交状态下,不同地区下存在相同目标终端+产品申请---------------");
                                    console.log(data);
                                    var  flag = data.flag;
                                    var  org = data.org;
                                    var  person = data.person;
                                    inList=eval(data.inList);
                                    outList=eval(data.outList);
                                    customerChannelFlag:data.customerChannelFlag;
                                    if(flag=="false"){
                                        _self.showWarning("checkRepeat","校验提醒","您添加的数据【"
										+ customer.name.l2 + "/"
										+ material.name.l2
										+ "】已被其他区域定义过:【"
										+ org.name.l2
										+ "/" + person.name.l2
										+ "】" );
                                    }
                                }
                            }); 
                        }

 

 

举报

相关推荐

0 条评论