.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="script/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
// ajax请求
$("#ajaxBtn").click(function(){
$.ajax({
url:"http://localhost:8080/json_ajax_i18n/ajax",//url 表示请求的地址
data:"action=jqueryAjax",//data表示发送给服务器的数据
//格式有两种:
//一:name=value&name=value
//二:{key:value}
type:"GET",
success:function (data){//success请求响应,响应的回调函数
//alert("返回的数据是:"+data);
//let parse = JSON.parse(data);
$("#msg1").html("ajax编号"+data.id+"姓名:"+data.name);
},
dataType:"json"//datatype响应的数据类型 常用的数据类型有text表示纯文本 xml表示xml数据json表示json对象
});
});
// ajax--get请求
$("#getBtn").click(function(){
//url 请求的url地址
//data 发送的数据
//callback 成功的回调函数
//type 返回的数据类型
$.get("http://localhost:8080/json_ajax_i18n/ajax","action=jqueryget",function (data){
$("#msg1").html("get编号"+data.id+"姓名:"+data.name);
},"json");
});
// ajax--post请求
$("#postBtn").click(function(){
// post请求
//url 请求的url地址
//data 发送的数据
//callback 成功的回调函数
//type 返回的数据类型
$.post("http://localhost:8080/json_ajax_i18n/ajax","action=jquerypost",function (data){
$("#msg1").html("post编号"+data.id+"姓名:"+data.name);
},"json");
});
// ajax--getJson请求
$("#getJSONBtn").click(function(){
// 调用
//url 请求的url地址
//data 发送给服务器的数据
//callback 成功的回调函数
$.getJSON("http://localhost:8080/json_ajax_i18n/ajax","action=jquerygetjson",function (data){
$("#msg1").html("getjson编号"+data.id+"姓名:"+data.name);
});
});
// ajax请求
$("#submit").click(function(){
// 把参数序列化
//serialize()可以把表单中所有表单项的内容都获取到,并以name=value& name=value的形式进行拼接
//alert($("#form01").serialize());
$.getJSON("http://localhost:8080/json_ajax_i18n/ajax","action=jqueryserialize&"+$("#form01").serialize(),function (data){
$("#msg1").html("serialize编号"+data.id+"姓名:"+data.name);
});
});
});
</script>
</head>
<body>
<div>
<button id="ajaxBtn">$.ajax请求</button>
<button id="getBtn">$.get请求</button>
<button id="postBtn">$.post请求</button>
<button id="getJSONBtn">$.getJSON请求</button>
</div>
<div id="msg1">
</div>
<br/><br/>
<form id="form01" >
用户名:<input name="username" type="text" /><br/>
密码:<input name="password" type="password" /><br/>
下拉单选:<select name="single">
<option value="Single">Single</option>
<option value="Single2">Single2</option>
</select><br/>
下拉多选:
<select name="multiple" multiple="multiple">
<option selected="selected" value="Multiple">Multiple</option>
<option value="Multiple2">Multiple2</option>
<option selected="selected" value="Multiple3">Multiple3</option>
</select><br/>
复选:
<input type="checkbox" name="check" value="check1"/> check1
<input type="checkbox" name="check" value="check2" checked="checked"/> check2<br/>
单选:
<input type="radio" name="radio" value="radio1" checked="checked"/> radio1
<input type="radio" name="radio" value="radio2"/> radio2<br/>
</form>
<button id="submit">提交--serialize()</button>
</body>
</html>
servlet程序
import com.atshangqiu.pojo.Person;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class AjaxServlet extends BaseServlet{
protected void jqueryAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("jqueryAjax方法调用了");
Person person=new Person(1,"bin");
Gson gson=new Gson();
String s = gson.toJson(person);
resp.getWriter().write(s);
}
protected void javaScriptAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("ajax请求过来了");
//返回字符串给客户端
//json格式的字符串
Person person=new Person(1,"chen");
Gson gs=new Gson();
String s = gs.toJson(person);
resp.getWriter().write(s);
}
protected void jqueryget(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("jqueryget方法调用了");
Person person=new Person(1,"bin");
Gson gson=new Gson();
String s = gson.toJson(person);
resp.getWriter().write(s);
}
protected void jquerypost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("jquerypost方法调用了");
Person person=new Person(1,"bin");
Gson gson=new Gson();
String s = gson.toJson(person);
resp.getWriter().write(s);
}
protected void jquerygetjson(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("jquerypost方法调用了");
Person person=new Person(1,"bin");
Gson gson=new Gson();
String s = gson.toJson(person);
resp.getWriter().write(s);
}
protected void jqueryserialize(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("jqueryserialize方法调用了");
System.out.println("用户名:"+req.getParameter("username"));
System.out.println("密码:"+req.getParameter("password"));
Person person=new Person(1,"bin");
Gson gson=new Gson();
String s = gson.toJson(person);
resp.getWriter().write(s);
}
}
配置web.xml
<servlet>
<servlet-name>AjaxServlet</servlet-name>
<servlet-class>com.atshangqiu.servlet.AjaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServlet</servlet-name>
<url-pattern>/ajax</url-pattern>
</servlet-mapping>