首页 > 代码库 > Jqueryd的一些 总结

Jqueryd的一些 总结

JSP层

   /*
发送data 主要有三种方式:
1、json 数组(推荐1)
2、url拼接
3、表单的序列化 serialize
*/

<script type="text/javascript" src="http://www.mamicode.com/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("img").click(function(){
$("input").css("border","3px dotted red");

$.ajax({
url:‘B‘,  //链接的servlet名字
//data:{userName:$(‘input‘).val()}, //传输的数据
data:$("input").serialize(),  //表单序列化传输数据
dataType:‘text‘,  //传输的类型
type:‘GET‘,  //传输方式
success:function(result){
alert(result);  //传输成功后 弹个窗
},
error:function(){
alert("发生错误...");  //失败了弹个窗
},
timeout:2000   //设定一个限时标准(超过就报错)
});
});
});
</script>
</head>
<body>
<form id="frm1">
<input type="text" name="userName" value="http://www.mamicode.com/text文本框" >
<img src="http://www.mamicode.com/5.jpg">
</form>
</body>

 

 

 

Servlet层

 

 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("访问doGet");
String userName = request.getParameter("userName");
PrintWriter out = response.getWriter();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("输出数据:"+ userName);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
System.out.println("访问doPost");
PrintWriter out = response.getWriter();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.println("在doPost输出数据:");

}

}

 

 

 

 

Jqueryd的一些 总结