Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

js gets the method of JSONArray of the map type returned by ajax


May 08, 2021 JSON



The code is as follows:
for(var i in datares){ 
if (datares.hasOwnProperty(i)) { 
sel.append("<option value='"+datares[i].deptid+"'>"+datares[i].deptname+"</option>"); 
} 

} 

ajax requests a map back and traverses it

$.ajax({
       type: "POST",
url: "<%=path%>/account/queryRolesAndGroups.action",
data: "enterpriseId="+eid,
success: function(msg){
var obj=eval("(" + msg + ")");
$.each(obj.groupList, function(i,item){      
alert(item.groupName); 
  })
}
})  
})

Send a request to the background, and then the background through a variety of operations, to get the corresponding data

If the data is map, map contains more than one list,

pw.print(net.sf.json.JSONObject.fromObject(returnMap.get("map")).toString());

The background is converted like this, and then the fore desk is converted: var obj=eval ("""

You can traverse the data inside (note: net.sf.json.JSONArray.fromObject ("list") so that it applies when converting a single collection, and then the fore desk traverses directly, without the need for a strong turn

success: function(msg){
alert(msg.length);
$.each(msg, function(i,item){      
alert(item.name); 
   })
}

JSONArray applies when turning a map that contains multiple lists.

There is also the use of background pw.print and pw.println should also note that when a single data 0,1 to the background judgment, pw.println will bring line breaks, never equal to 0,1.

response.setContentType("text/plain;charset=UTF-8");
     PrintWriter pw =response.getWriter();

These two sentences should also pay special attention to the order of return garbled processing.

PrintWriter pw =response.getWriter(); In the first words, the conversion also does not work!