jquery实现ajax

 ajax() 方法用于执行 AJAX(异步 HTTP)请求。

参考文档http://www.runoob.com/jquery/ajax-ajax.html

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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 type="text/javascript" src="js/jquery-3.3.1.js"></script>
<style>
#showData{
	width: 200px;
	height: 100px;
	border: 1px solid black;
}
</style>
<script>
	function getData(){
		$.ajax({//常用的一些参数就这些。
			url : 'GetDataServlet',//url	规定发送请求的 URL。默认是当前页面。
			type : 'GET', //type	规定请求的类型(GET 或 POST)。
			data : {},//data	规定要发送到服务器的数据。    
			dataType : 'json',//dataType	预期的服务器响应的数据类型。
			success : function(result){//success(result,status,xhr)	当请求成功时运行的函数
		        $('#showData').html(result);
		    },
		    error:function(){//error(xhr,status,error)	如果请求失败要运行的函数。
		    	
		    }
		})
	}
</script>
</head>
<body>
<button onclick="getData()">获取数据</button>
<div id="showData"></div>	
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_37311400/article/details/84587644