您的位置:首页 > Web前端 > JavaScript

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());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: