JSP + AJAX完整实例及代码

(1)发送请求index.jsp,注意引入jquery.min.js文件

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="GB18030"%>
<!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>Insert title here</title>
<script src="jquery.min.js"></script>
<script>
jQuery(document).ready(function($) {
$('#send').click(function(){
$.ajax({
type: "POST",
url: "ajax.jsp",
async:false,
data: {name:"name", content:"content"},
// dataType: "json",
dataType: "text",
success: function(data){ //alert(JSON.stringify(data));//使用JSON方法,需引入jar包,json-java.jar、json-lib.jar
alert(data);
},
error: function(e) {
alert(e);
}
});
});
});
</script>
</head>
<body>
<div>
<button id="send">test</button>
</div>
</body>
</html>

(2)接受请求并返回值ajax.jsp

<%@ page contentType="text/html; charset=gb2312" %>
<%
String id=request.getParameter("name");
System.out.println("Parameter is :"+id);
//out.print("{3:8,9:4}");//json
out.print("sdfwsuueiw");
%>
上一篇:DataTables 控件使用和心得 (2) - 参数Options


下一篇:android 调用地图