1.引入jar包

2.編寫方法
//處理器方法返回值void
@RequestMapping("/first")
public void doFirst(HttpServletResponse response) throws IOException {
//要返回給瀏覽器的數據
List<String> list=new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
//使用輸出流返回給瀏覽器
String jsonString = JSON.toJSONString(list);
PrintWriter writer = response.getWriter();
writer.write(jsonString);
}
3.編寫jsp頁面
<%@page language="java" pageEncoding="utf-8" contentType="text/html; utf-8" %>
<html>
<head>
<%--引入js--%>
<script type="text/javascript" src="js/jQuery1.11.1.js"></script>
<script type="text/javascript">
$(function () {
//注冊按鈕單擊事件
$("input").click(function () {
$.ajax({
//請求地址
url:"/first",
//請求方式
type:"POST",
//回調函數
success:function (data) {
//使用each方法遍歷
$.each(eval("("+data+")"),function (i,item) {
alert(item);
})
}
});
})
});
</script>
</head>
<body>
<h2>Hello World!</h2>
<input type="button" value="使勁點我!!!"/>
</body>
</html>
測試結果

8月ECS限量搶,全場ECS 2折起,數量有限,先到先得,搶完為止。https://www.aliyun.com/
