jsp页面发送ajax请求实例
2013-07-30 10:13
531 查看
jsp页面加载完毕发送ajax请求:
$(function() {
$.ajax( {
url : '/internate/allProduct.action',
type : 'post',
data:{'limit':8},
success : function(data) {
var html="<ul class='clearfix'>";
for(var i=0;i<data.length;i++){
html+="<li> <a href='products.jsp' title='"+data[i].product_name+"'><img src='uploadfile/"+data[i].product_image+"' alt='"+data[i].product_name+"' width='154' height='110' /><span>"+data[i].product_name+"</span> </a></li>";
}
html+="</ul>";
$("#all").html(html);
},
dataType : 'json'
});
});
后台程序处理:
if(path.equals("allProduct")){
int limit=Integer.parseInt(request.getParameter("limit"));
System.out.println(limit);
IndexDAO dao=new IndexDAOImpl();
List<Product> proList=new ArrayList<Product>();
try {
proList=dao.allProduct(limit);
} catch (Exception e) {
System.out.println("数据库异常!");
e.printStackTrace();
}
JSONArray obj=JSONArray.fromObject(proList);
out.print(obj.toString());
}
使用的是servlet+jsp+ajax技术,其中要用到json需要导入对应的jar包;回调函数中的data,就是out.print(obj.toString());
$(function() {
$.ajax( {
url : '/internate/allProduct.action',
type : 'post',
data:{'limit':8},
success : function(data) {
var html="<ul class='clearfix'>";
for(var i=0;i<data.length;i++){
html+="<li> <a href='products.jsp' title='"+data[i].product_name+"'><img src='uploadfile/"+data[i].product_image+"' alt='"+data[i].product_name+"' width='154' height='110' /><span>"+data[i].product_name+"</span> </a></li>";
}
html+="</ul>";
$("#all").html(html);
},
dataType : 'json'
});
});
后台程序处理:
if(path.equals("allProduct")){
int limit=Integer.parseInt(request.getParameter("limit"));
System.out.println(limit);
IndexDAO dao=new IndexDAOImpl();
List<Product> proList=new ArrayList<Product>();
try {
proList=dao.allProduct(limit);
} catch (Exception e) {
System.out.println("数据库异常!");
e.printStackTrace();
}
JSONArray obj=JSONArray.fromObject(proList);
out.print(obj.toString());
}
使用的是servlet+jsp+ajax技术,其中要用到json需要导入对应的jar包;回调函数中的data,就是out.print(obj.toString());
相关文章推荐
- jsp发送ajax请求,servlet返回json对象实例
- java web项目中的jsp页面发送ajax请求404错误
- JSP页面Ajax发送请求与乱码解决
- jsp页面发送ajax请求遮罩层显示失败,或者请求完才显示
- 从jsp页面发送ajax请求,servlet接受参数并返回json数据
- Ajax发送POST请求SpringMVC页面跳转失败
- JSP中在提交表单之前,发送ajax请求进行js的验证
- javascript与jsp发送请求到servlet的几种方式实例
- Javascript发送AJAX请求实例代码
- (2)设计登录页面和发送Ajax请求
- ajax 页面请求后,jsp页面定位
- JSP页面 jquery发送post请求到 contrallor 接受JSON数据
- jsp中自动重新发送请求刷新当前页面
- jqury+ajax每秒向后台发送请求数据然后返回页面(包括jqruy页面加载完毕才执行方法)
- java----jsp发送请求到后台进行页面的跳转
- jquery+ajax每秒向后台发送请求数据然后返回页面的代码
- jQuery,ajax请求返回长数字字符串,jsp页面不显示为科学计数法
- ajax提交表单碰到页面刷新和发送两次请求的问题
- 发送ajax请求的时候,如果页面卡住,可能是data数据的问题。
- Ajax发送POST请求SpringMVC页面跳转失败