使用ajax异步加载html数据给后台jsp,将数据存到数据库(ajax还会停留在当前的页面,不会发生跳转,但是数据也已经传到了后台jsp,可以继续进行数据库的操作)
前端中有两个文本框,需要在点击按钮的时候,将文本框的内容通过ajax传递给后台jsp页面 post.jsp
前端代码:
<input type="text" class="qq" value=""> <input type="text" class="phone" value=""> <button type="button" class="btn">发送请求</button>
js部分代码为:
$('.btn').click(function () {
$.ajax({
type:"post",
url:"./post.jsp",
dataType:"text",
data:{
qq:$(".qq").val(),
phone:$(".phone").val()
},
success : function(data) {
alert('接收成功 ');
},error : function() {
alert('接收失败 ');
}
});
})
post.jsp:
String qq = request.getParameter("qq");
String phone = request.getParameter("phone");