var url = "../searchclasses"; $.ajax({ url: url, type: "post", dataType: "json",//以json形式接收 error: function() { alert("error"); }, success: function(data) { var html = ""; $.each(data, function(index, content) { var kch = content.KCH, kcm = content.KCM, ks = content.KS, kcdm = content.KCDM; html = kch + " " + kcm + " " + ks + " " kcdm; $("#main .main-serarch-box").append(html); }); } });
加个error:function(){
alert("error");
}
来判断是否成功接收到了json数据
后台组装json
out.print("["); for (int i = 1; i <= totalRecord && boo; i++) { int kch = rowSet.getInt(1); String kcm = rowSet.getString(2); Float ks = rowSet.getFloat(3); String kcdm = rowSet.getString(4); if (i != totalRecord) { out.println("{ \"KCH\" : \"" + kch + "\" , \"KCM\" : \"" + kcm + "\",\"KS\" : \"" + ks + "\" , \"KCDM\" : \"" + kcdm + "\"},"); } else { out.println("{ \"KCH\" : \"" + kch + "\" , \"KCM\" : \"" + kcm + "\",\"KS\" : \"" + ks + "\" , \"KCDM\" : \"" + kcdm + "\"}"); } // 这里出现了小问题,i的最后一个判断之前没搞清 boo = rowSet.next(); } out.print("]");
复制开发者工具栏的json数据到 http://www.bejson.com/
json工具检验网站检验下,检查从前台收到的data是否真的是json数据,
json的数组格式为 [{},{},{}]
之前因为少了一个[] 导致error回调函数接收到的为error;
接收成功就是根据json对象来调用json数据
$.each(data, function(index, content) { var kch = content.KCH, kcm = content.KCM, ks = content.KS, kcdm = content.KCDM; html = kch + " " + kcm + " " + ks + " " kcdm; $("#main .main-serarch-box").append(html); });
然后拼装数据就就可以了。