JS传递对象数组为参数给后端,后端获取的实例代码_javascript技巧_脚本之家

作者:运维知识    发布时间:2019-12-11 14:22     浏览次数 :

[返回]

前端JS代码:

JS传递对象数组为参数给后端,后端获取,js后端

前端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);

前端JS代码: var conditons = []; var test1 = new Object(); test1.name="1"; test1.id="2"; var test2 = new Objec...

前端JS代码:

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

  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");
      }
    });
  });