1. 程式人生 > 實用技巧 >(傲嬌的白狐)前端獲取json資料 個人記錄

(傲嬌的白狐)前端獲取json資料 個人記錄

前端獲取json資料 後端寫成這樣的話

@RequestMapping("/insertreimbursement")
    @ResponseBody
    public String insertreimbursement(Reimbursement reimbursement,HttpSession session){
略。。。。。。。。。。。。。。。。。。。。
if(i != 0){
jsonData = JsonData.buildSuc("新增成功,等待稽核");
}else {

jsonData = JsonData.buildError("錯誤");

}
System.out.println("JSON.toJSONString(json) = " + JSON.toJSONString(jsonData));

System.out.println("jsonData = " + jsonData);
return JSON.toJSONString(jsonData);


}

返回的是 string型別的 而一些前端框架 會把strign 轉換為ison型別 普通使用要 這樣

$.post({
                url:'/reimbursement/insertreimbursement',
                data:{'description':description,
                    'tyname':tyname,'TypesId':typesId,'money':money,'certificate':im
                },
                success:function (re) {

                    var jsonobj
= eval('(' + re + ')'); console.log(re) console.log(jsonobj) console.log(typeof re) console.log(jsonobj.msg) if(re.code==200){ alert(re.msg) window.location.href
="/page/defaut"; return false; }else { alert(re.msg); return false; } } },"json")