导入JS文件
load方法的使用
先引入js文件
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
function load() {
//等同于document.getElementById,从/myAjax/Demo04(Servlet)中获取数据闭关赋值给id为text01的输入框中
$("#text01").load("/myAjax/Demo04",function(responseTxt ,statusTxt ,xhr){
$("#text01").val(responseTxt);
});
}
</script>
<h3><input type="buttom" onclick="load()" value="使用JQuery执行load方法"></h3>
<input type="text" id="text01">
get()方法
<script type="text/javascript">
function get() {
$.get("/myAjax/Demo04",function(data,status){
//有value属性才可以用val(),html()可以设置颜色灯Html属性,text()
$("#div1").html(data);
});
}
</script>
<title>Insert title here</title>
</head>
<body>
<input type="button" id="b1" onclick="get()"value="点击使用JQ的get方法" >
<div id="div1" style="height:100px;width:100px;border:1px solid #000">
</div>
</body>
Servlet中
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("给你一份数据");
post()方法
<script type="text/javascript">
function post(){
$.post("/myAjax/CheckUsernameServlet",
{
name:"zcbb",
age:"lxmg"
},
function(data,status){
$("#div1").html(data)
});
}
</script>
<title>Insert title here</title>
</head>
<body>
<input type="button" id="b1" onclick="post()"value="点击使用JQ的get方法" >
<div id="div1" style="height:100px;width:100px;border:1px solid #000">
</div>
</body>