JS passes the object array as a parameter to the back end and the back end obtains the instance code

  • 2021-07-01 06:25:04
  • OfStack

Front-end JS code:


    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:' Link ', 
                data:{name:"123",conditions:JSON.stringify(conditons)},
                dataType : 'json', 
                success:function(data){
                      console.log(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown){
                      alert("error");
                }
          });
    });

Important note: Convert the array of objects to a string in the form of JSON: JSON. stringify

Back-end acquisition:


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

Related articles: