json原理分析及實例介紹
更新時間:2012年11月29日 10:30:05 投稿:whsnow
這次在項目中前后臺的數(shù)據(jù)交互中用到了json,經(jīng)過這段時間的使用,簡單總結(jié)一下json的原理與使用,需要了解的朋友可以參考下
這次在項目中前后臺的數(shù)據(jù)交互中用到了json,經(jīng)過這段時間的使用,大概了解了一下,簡單總結(jié)一下json。
復(fù)制代碼 代碼如下:
@RequestMapping("/work/plan/checkSubmitForApproval")
public void checkSubmitForApproval(String planId,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
String result="{\"result\":\"faild\",\"personSituation\":\"null\"}";
HttpSession session = request.getSession();
String industryID = (String) session.getAttribute("industryID");
IIndustry industry = industryService.getById(industryID);
if(industry.getType().equals("XXX")){
try {
boolean flag = false;
IProjectMain yearPlan = projectPlanService.findProjectPlanById(planId);
List<IStaffInfo> listStaffInfo = sysStaffService.getStaffByPlanId(planId, industryID);
for(int i=0;i<listStaffInfo.size();i++){
if(listStaffInfo.get(i).getPractitionersPost().equals(StaffRole.PROGECTMANAGER.toString())){
flag = true;
}
}
if(flag == true){
result="{\"result\":\"success\",\"personSituation\":\""+yearPlan.getPerson_Situation()+"\"}";
}else{
result="{\"result\":\"success\",\"personSituation\":\""+yearPlan.getPerson_Situation()+"\",\"isManager\":\"false\"}";
}
} catch (Exception e) {
result="{\"result\":\"falid\"}";
throw new PlatformException(e);
}finally{
OutputUtils.write(response,result,"text/x-json;charset=UTF-8");
}
先PutputUtils中的write代碼:
復(fù)制代碼 代碼如下:
public static void write(HttpServletResponse response, String text, String contentType)
{
PrintWriter out=null;
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType(contentType);
try
{
out = response.getWriter();
out.write(text);
}
catch (IOException e)
{
Logger.getLogger(OutputUtils.class).error(e.getMessage(), e);
} finally{
if(out!=null){
out.flush();
out.close();
}
}
}
with (document.getElementById("planForm")) {
action=驗證合法后要提交的url;
method="post";
submit();
}
<span style="white-space:pre"> </span>}
其中success:function(data)是一個回調(diào)函數(shù),即上面做的驗證action的方法成功之后執(zhí)行的操作。
關(guān)于ajax和jquery的歷史,寫的很清楚。
jquery已經(jīng)封裝好了從response中取data的操作,所以這里用起來非常方便,省去了從xml中一點一點讀取的頭疼,給開發(fā)帶來了極大方便。
相關(guān)文章
JavaScript基于面向?qū)ο髮崿F(xiàn)的猜拳游戲
這篇文章主要介紹了JavaScript基于面向?qū)ο髮崿F(xiàn)的猜拳游戲,結(jié)合完整實例形式分析了javascript基于面向?qū)ο髮崿F(xiàn)猜拳游戲的具體頁面布局、樣式及功能相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
深入理解JS中attribute和property的區(qū)別
property 和 attribute非常容易混淆,但實際上,二者是不同的東西,屬于不同的范疇,本文就詳細的介紹一下JS中attribute和property的區(qū)別 ,感興趣的可以了解一下2022-02-02
IE圖片緩存document.execCommand("BackgroundImageCache",
IE6下設(shè)置背景圖片是不會被真正cache住的,就算服務(wù)器做了cache,如果想cache住只能2011-03-03
JS根據(jù)瀏覽器窗口大小實時動態(tài)改變網(wǎng)頁文字大小的方法
這篇文章主要介紹了JS根據(jù)瀏覽器窗口大小實時動態(tài)改變網(wǎng)頁文字大小的方法,涉及JavaScript針對頁面寬高的動態(tài)獲取與元素樣式動態(tài)運算的相關(guān)技巧,需要的朋友可以參考下2016-02-02

