jQuery框架的ajax

三种常见的请求

请求方式语法
GET请求$.get(url, [data], [callback], [type])
POST请求$.post(url, [data], [callback], [type])
AJAX请求$.ajax([settings])

简单的get请求

参数名称解释
url请求的服务器端url地址
data发送给服务器端的请求参数 ,格式可以是key=value,也可以是js对象
callback当请求成功后的回掉函数,可以在函数体中编写我们的逻辑代码
type预期的返回数据的类型,取值可以是 xml, html, script, json, text, _defau
<script type="text/javascript">
function sendRequest(){
		$.get(
			"/AjaxDemo/ajaxServlet",
			"name=tom&age=33",
			function(data){
				alert(data);
			},
			"text"
			);
		}
</script>
</head>
<body>
<input type="button" value="ajax异步访问服务器端" onclick="sendRequest()">
</body>

jsp

public class AjaxServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//获得请求参数
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		response.getWriter().write("ajax response data ..."+ name +"..."+age);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
		doGet(request, response);
}
}

在这里插入图片描述

简单的post请求

参数名称解释
url请求的服务器端url地址
data发送给服务器端的请求参数 ,格式可以是key=value,也可以是js对象
callback当请求成功后的回掉函数,可以在函数体中编写我们的逻辑代码
type预期的返回数据的类型,取值可以是 xml, html, script, json, text, _defau

和get几乎是一样的 就不重写了

*AJAX请求方式

jQuery.ajax([settings])
settings常见的属性如下

属性名称解释
url请求的服务器端url地址
async(默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为false
data发送到服务器的数据,可以是键值对形式,也可以是js对象形式
type(默认: “GET”) 请求方式 (“POST” 或 “GET”), 默认为 “GET”
dataType预期的返回数据的类型,取值可以是 xml, html, script, json, text, _defaul等
success请求成功后的回调函数
error请求失败时调用此函数

js代码

<script type="text/javascript">
function sendRequest(){
		$.ajax({
			url	:	"/AjaxDemo/ajaxServlet",
			async:true,
			data:"name=tom&age=33",
			type:"GET",
			dataType:"text",
			success:function(data){
				alert(data);
			},
			error:function(){
				alert("数据没有成功返回!")
			}
			});
		}
</script>
<body>
<input type="button" value="ajax异步访问服务器端" onclick="sendRequest()">
</body>

Servlet代码

public class AjaxServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
		//获得请求参数
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		response.getWriter().write("ajax response data ..."+ name +"..."+age);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
		doGet(request, response);
}
}

效果
在这里插入图片描述

jQuery3.0 为get和post新增了签名方式 让其和.ajax保持一致 参数都是一样的

演示3.0中的post
$.post([settings])

属性名称解释
url请求的服务器端url地址
async(默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为false
data发送到服务器的数据,可以是键值对形式,也可以是js对象形式
type(默认: “GET”) 请求方式 (“POST” 或 “GET”), 默认为 “GET”
dataType预期的返回数据的类型,取值可以是 xml, html, script, json, text, _defaul等
success请求成功后的回调函数
error请求失败时调用此函数

js代码

<script type="text/javascript">
function sendRequest(){
		$.post({
			url:"/AjaxDemo/ajaxServlet",
			async:true,
			data:"name=tom&age=33",
			type:"GET",
			dataType:"text",
			success:function(data){
				alert(data);
			},
			error:function(){
				alert("数据没有成功返回!")
			}
			});
		}
</script>
</head>
<body>
	<input type="button" value="ajax异步访问服务器端" onclick="sendRequest()">
</body>

Servlet代码

@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
		//获得请求参数
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		response.getWriter().write("ajax response data ..."+ name +"..."+age);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
	doGet(request, response
);

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值