因为某些兼容性的问题,我们通常会使用jquery来写ajax,要使用jquery首先我们需要导入jquery.
<script type="text/javascript" src="http://localhost:8080/jquery_test/jquery.js"></script>
以下是html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>first web</title>
<link rel="stylesheet" href="./index.css">
<script type="text/javascript" src="http://localhost:8080/jquery_test/jquery.js"></script>
<script src="http://localhost:8080/jquery_test/test.js"></script>
</head>
<body>
<div>
<h1 id="g1">欢迎进入我的网站</h1>
<div id="k1">
<form action="">
<div>内容:<textarea name="content" rows="8" cols="30" id="content"></textarea></div>
<div id="ss"></div>
</form>
<button id="t1">点击</button>
<p></p>
</div>
</div>
</body>
</html>
以下是js代码
$(function(){
$("#t1").click(function(){
$.ajax({
url:"http://localhost:8080/jquery_test/Use_ajax",
error:function(){
alert("请求失败");
},
success:function(data){
alert(JSON.stringify(data));
alert(new Date());
},
type:"get",
dataType:"json",
data:{
a:18,
name:17,
date:new Date()
}
});
});
});
url:请求地址
error:请求失败响应
success:请求成功响应
type:响应类型
dataType:返回类型,一般为json型或字符串型
data:传参,格式为json格式
java代码和上一期使用ajax一样