我在JSP中遇到了自定义标记的以下问题。页面加载结束时div自定义标记生成的html内容列表。之后,我想用ajax的新数据替换列表。首先,我清空旧列表。我做了一个HTML字符串(pdListHtml),追加它。但我根本找不到任何新清单。请指出代码是否有问题。谢谢。
html.jsp
<ul class="pdImgType">
<c:forEach var="goods" items="${goods}">
<div class="pdList">
<pt:product prdNm="${goods.goods_name}" dispPrc="${goods.goods_origin_price }"/>
</div>
</c:forEach>
</ul>
Ajax JS
const filterParam = $("#submitForm").serializeArray();
$.ajax({
type : "post",
url : "/ctgrSearchFilter.do",
data : filterParam,
dataType : "json",
success : function(data) {
$(".pdImgType").empty();
const goods = data.result.goods;
for(let i=0 ; i < goods.length ; i++){
let pdListHtml = "";
pdListHtml += '<div class=\"pdList\">';
pdListHtml += '<pt\:product prdNm=\"'+ goods[i].goods_name +'\" dispPrc=\"'+ goods[i].goods_origin_price +'\"/>';
pdListHtml += '</div>';
$(".pdImgType").append(pdListHtml);
}
},
error : function(err) {
alert("error");
}
});
来自Java的数据
@RequestMapping(value = "/ctgrSearchFilter.do" , method = equestMethod.POST)
@ResponseBody
public JSONObject ctgrSrhFilter(HttpServletRequest request) throws Exception {
JSONObject json = new JSONObject();
ModelAndView mv = commSearchService.callSearchAllNewAPI(request);
logger.info(">>>> ctgr result mv : "+ mv);
json.put("result", mv.getModel());
return json;
}
控制台登录Java
ctgr结果mv:ModelAndView:物化视图为[null];模型是{cateCntTot = 0,cate = [],brand1List = [],delryList = [],总数= 1254,goods = [{goods_name = nice,goods_origin_price = 109000}]}]
您需要使用goods
获取data.goods
,然后在其上循环。此外,请检查json
是否有效。
演示代码:
//your json response
var data = {
"cateCntTot": 0,
"cate": [],
"brand1List": [],
"delryList": [],
"total": 1254,
"goods": [{
"goods_name": "nice",
"goods_origin_price": 109000
}]
};
var pdListHtml = "";
///fetching data->goods
var goods = data.goods;
for (let i = 0; i < goods.length; i++) {
//your html
/*pdListHtml += '<div class=\"pdList\" style="background:yellow"><pt\:product prdNm=\"' + goods[i].goods_name + '\" dispPrc=\"' + goods[i].goods_origin_price + '\"/></div>';*/
//demo data to append
pdListHtml += '<div class=\"pdList\" style="background:lightblue">prdNm :' + goods[i].goods_name + ' <br/>dispPrc :' + goods[i].goods_origin_price + '</div>'
}
//add data under pdImgType class
$(".pdImgType").html(pdListHtml);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="pdImgType">
<c:forEach var="goods" items="${goods}">
<div class="pdList">
<pt:product prdNm="${goods.goods_name}" dispPrc="${goods.goods_origin_price }" />
</div>
</c:forEach>
</ul>