jsp页面发送ajax请求实例

来源:互联网 发布:web编程入门c# 编辑:程序博客网 时间:2024/06/06 02:22

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());

原创粉丝点击