解決後臺傳輸list到前臺以json格式
阿新 • • 發佈:2019-02-05
我用的框架是springmvc+hibernate,前段用easyui。
後臺經過查詢後得到一個list資料。
controller類:
@RequestMapping("/getOrderInfo") @ResponseBody public void getOrderInfo(SearchDto searchDto, HttpServletRequest request, HttpServletResponse response, OrderDto orderDto) { String issueNo = request.getParameter("issueNo"); List<OrderDto> list = orderService.getOrderInfo(orderDto,issueNo); try { ControllerUtils.printJson(list); } catch (Exception e) { e.printStackTrace(); } }
ControllerUtils類 printJson方法:
public static void printJson(Object entity)throws Exception{
String json="";
if(entity!=null){
json=JsonUtil.getJson(entity);
}
LotContext.getResponse().getWriter().write(json);
}
前臺得到的資料用firebug檢視時一堆字串,不是正確的json格式資料。
此時只需要在前臺jsp頁面用eval方法解析資料就可以了。
function searchOrder(){ $.messager.progress(); // 顯示進度條 $('#ff').form('submit', { url: 'getOrderInfo.htm', onSubmit: function(){ var isValid = $(this).form('validate'); if (!isValid){ $.messager.progress('close'); // 如果表單是無效的則隱藏進度條 } return isValid; // 返回false終止表單提交 }, success: function(result){ //result是後臺返回的資料 var data = eval('(' + result + ')') //關鍵程式碼 $.messager.progress('close'); // 如果提交成功則隱藏進度條 console.info('data',data); $('#tt').datagrid('loadData', data); } }); };
然後資料就可以正確的以json格式得到了。
如果有錯誤,望指出,讓大家一同學習。