1.前端Ajax请求如下:
$.ajax({
type : "post",
//async:false,
url : PATH + "/product/selectPicture.action",
dataType : "json",
success : function(data) {
$.each(data,function(k,v){
alert(k +":"+ v);
});
}
});
2 其中PATH在jsp页面中定义
<%
String path=request.getContextPath();
%>
<script>
var PATH = ‘<%=path%>';
</script>
3. 后端java代码输出json格式数据:
List<Map<String,Object>> list = bo.selectData();
response.setContentType("application/json;charset=GBK");
PrintWriter out=response.getWriter();
out.write(JSONArray.fromObject(list).toString());
out.flush();
out.close();
其中list为从数据库中查出的数据,
JSONArray.fromObject(list)需要导入json-lib-2.4-jdk15.jar包.
注意返回的格式为json,
和编码格式。