SpringMVC处理器方法返回值void ajax方式
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/

浙公网安备 33010602011771号