js handles strings in json format

The background returns a json string:

@RequestMapping(value = "/getDimensionByPid", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
@ResponseBody
public String getDimensionByPid(HttpServletRequest request,
		@RequestParam(value = "pid", required = true) String pid) {
		log.info("**pid:{}", pid);
		List<PyDimension> dimensionList = this.pydimensionService.getByPid(pid);
		log.info("**dimensionList:{}", dimensionList.toString());
		JSONArray jsonArray = new JSONArray();
		jsonArray.addAll(dimensionList);
		log.info("**jsonArray:{}", jsonArray.toString());
		return jsonArray.toString();
	}

 The returned json string:

**jsonArray:[{"id":"xxx","name":"xxx","note":"xxx","pid":"xxx","weight":0.2}]

Frontend js:

<script type="text/javascript">
	function getDimensionByPid () {
		var pid = $("#projectId").val();
		$.ajax({
	            type: "POST",
	            contentType:'application/x-www-form-urlencoded;charset=utf-8',
	            url:"${basePath}pynorm/getDimensionByPid",
	            data:"pid="+pid,
	            dataType:'json',
	            success:function(data){    
	                $("#dimensionId").empty();
	                var len = eval(data).length;
	                for (var i = 0; i <len; i ++) {
	                    var id = data[i].id;
		            var name = data[i].name;
		            $("#dimensionId").append("<option value='"+id+"'>"+name+"</option>");
	                    }
	                }
	            });
		}
	</script>

 Convert the json string received from the background into a json object through the eval() method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326316181&siteId=291194637