直接先贴代码
$.ajax({
type:'post',
dataType: "json",
url:url,
contentType: 'application/json;charset=utf-8',
data:JSON.stringify(datas),
success: function(result){
alert(result[0].priceId);
alert(result[0].pricemoney);
$.each(result, function(index, item){
findTableQuery += '<tr>'+
'<td>'+item.goods.goodsname+'</td>'+
'<td>'+item.pricemoney+'</td>'+
'<td>'+item.date+'</td>'+
'</tr>'
});
//清空上次数据
$("#tableQuery tBody tr:not(:first)").html("");
//插入新查询数据
$("#tableQuery #trQuyer").after(findTableQuery);
}
后台代码
@RequestMapping(value="/recordQuery")
@ResponseBody
public List<Price> getPriceList(@RequestBody String[] array){
int areaId = Integer.parseInt(array[0]);
String goodsName = array[1];
//查询物品是否存在
Goods goods = this.goodsService.getGoodsBy(goodsName);
//查询物品记录
List<Price> priceList = this.priceService.getPriceByList(areaId,goods.getGoodsId());
for(Price price : priceList){
System.out.println(price.getArea().getAreaName()+"-"+
price.getGoods().getGoodsName()+"-"+
price.getPriceMoney()+"-"+price.getDate());
}
return priceList;
}
以下是调试js时查看到result的返回数据
Array(2)
0:{priceId: 1, priceMoney: "3246", date: 1534464000000, areaId: null, goodsId: null, …}
1:{priceId: 2, priceMoney: "3084", date: 1534550400000, areaId: null, goodsId: null, …}
length:2
__proto__:Array(0)
我的问题就是,为什么我获取到priceId是没有问题的,但获取priceMoney得到的确是undefined。
有没有大佬可以告诉我这该怎么解决,百度了老半天,各种五花八门的方法都用遍了,结果还是这样。
每次出问题,在CSDN上询问,都是石沉大海,难道CSDN已经无人了?我一个人在玩单机?
本文详细探讨了一个关于前后端数据交互的问题,即在使用$.ajax进行异步请求时,如何正确解析从Java后台返回的JSON数据。具体地,尽管能成功获取到priceId,但在尝试访问priceMoney字段时却遇到undefined错误。文章深入分析了这一现象的原因,并提供了有效的解决方案。
4万+

被折叠的 条评论
为什么被折叠?



