获取json的ajax


jquery部分

$.getJSON("<c:url value='/props.do'/>?method=loadCate&cateBigId="+cateBigId+"&curDate="+new Date(),function(data) {

var oSheng = $("#cateId");
    oSheng.empty();//清空select下拉框
   
if($.isArray(data["cateid"])){//是否为数组

for(var i=0;i<data["cateid"].length;i++){
var option = $("<option value='"+data["cateid"][i]+"'>"+data["catename"][i]+"</option>");

if(data["cateid"][i] == ${props.propsCate.cateId + 0}){ //子项是否选中
option.attr("selected",true);
}

option.appendTo(oSheng); //动态添加Option子项
}
}else{

var option = $("<option value='"+data["cateid"]+"'>"+data["catename"]+"</option>");
if(data["cateid"] == ${props.propsCate.cateId + 0}){ //子项是否选中
option.attr("selected",true);
}
option.appendTo(oSheng);
}

});


java部分

public void loadCate(HttpServletRequest request, HttpServletResponse response){
    response.setCharacterEncoding("UTF-8");
    int cateBigId = StringHelper.toInt(request.getParameter("cateBigId"));
    StringBuffer hql = new StringBuffer("FROM WPropsCate WHERE propsCateBig.cateBigId = ").append(cateBigId);
   
    JSONObject jsonObject= new JSONObject();
try {
List list = propsService.getPageWProps(hql.toString());
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
WPropsCate cate = (WPropsCate) iterator.next();
jsonObject.accumulate("catename", cate.getCateName());
jsonObject.accumulate("cateid", cate.getCateId());

}
response.getWriter().print(jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
    }

你可能感兴趣的:(jquery,Ajax,json)