HTML中异步接收返回的json字符串输出

如果返回的json是一个list集合,则需要在js中编写循环语句输出,代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>异步接收集合数据</title>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
  $(function(){
	  $("#btn").click(function(){
		  $.getJSON("ajax.do",function(json){
			   $(json).each(function(index,user){
                    $("#xianshi").append("<br>"+user.uname+","+user.psw);	
		  }) 
	  })
  })
})
</script>
</head>
<body>
   <button id="btn">点击异步</button>
   <div id="xianshi">
   </div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_36677358/article/details/84554277
今日推荐