JSON——後端向前臺 select 標籤傳遞 list json型別物件時,前臺的處理
阿新 • • 發佈:2018-12-20
獲取 後臺的list json 物件 1.格式化 data 的json 資料
var json= JSON.stringify(data); 2.轉 json 資料
var a = eval(json);
3.遍歷 json 物件
$.each(a,function(index,item){
$("#sc_category").append('<option value="'+ item.categoryName +'">' + item.categoryName + '</option>');
});
1.前臺處理程式碼
$(function () { getProductCategory(); }); function getProductCategory(){ $.ajax({ url: "/generalManager/getProductCategory", //後臺url type: "GET", dataType: "json", success: function (data) { var json= JSON.stringify(data); console.log("////"+json); var a = eval(json); $.each(a,function(index,item){ console.log("....."+item.categoryName); $("#sc_category").append('<option value="'+ item.categoryName +'">' + item.categoryName + '</option>'); }); }, error: function (er) { //失敗,回撥函式 // alert('修改錯誤'); alert(er) } }); }
2.後臺傳遞 list json 物件
public void getProductCategory( HttpSession session, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{ System.out.println("**************"); List<ProductCategory> list = productCategoryService.selectByExample(null); SetEncoding.getResponse(response);//設定utf-8編碼 String categoryList = JSON.toJSONString(list); try { // 將後臺資訊傳至前臺 PrintWriter out = response.getWriter(); out.println(categoryList); System.out.println(categoryList); out.flush(); out.close(); } catch ( IOException e) { e.printStackTrace(); } }