json原理分析及實(shí)例介紹
這次在項(xiàng)目中前后臺(tái)的數(shù)據(jù)交互中用到了json,經(jīng)過(guò)這段時(shí)間的使用,大概了解了一下,簡(jiǎn)單總結(jié)一下json。
@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代碼:
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=驗(yàn)證合法后要提交的url;
method="post";
submit();
}
<span style="white-space:pre"> </span>}
其中success:function(data)是一個(gè)回調(diào)函數(shù),即上面做的驗(yàn)證action的方法成功之后執(zhí)行的操作。
關(guān)于ajax和jquery的歷史,寫(xiě)的很清楚。
jquery已經(jīng)封裝好了從response中取data的操作,所以這里用起來(lái)非常方便,省去了從xml中一點(diǎn)一點(diǎn)讀取的頭疼,給開(kāi)發(fā)帶來(lái)了極大方便。
相關(guān)文章
JavaScript函數(shù)中this指向問(wèn)題詳解
這篇文章主要給大家介紹了關(guān)于JavaScript函數(shù)中this指向問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
JavaScript基于面向?qū)ο髮?shí)現(xiàn)的猜拳游戲
這篇文章主要介紹了JavaScript基于面向?qū)ο髮?shí)現(xiàn)的猜拳游戲,結(jié)合完整實(shí)例形式分析了javascript基于面向?qū)ο髮?shí)現(xiàn)猜拳游戲的具體頁(yè)面布局、樣式及功能相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
深入理解JS中attribute和property的區(qū)別
property 和 attribute非常容易混淆,但實(shí)際上,二者是不同的東西,屬于不同的范疇,本文就詳細(xì)的介紹一下JS中attribute和property的區(qū)別 ,感興趣的可以了解一下2022-02-02
JS取數(shù)字小數(shù)點(diǎn)后兩位或n位的簡(jiǎn)單方法
下面小編就為大家?guī)?lái)一篇JS取數(shù)字小數(shù)點(diǎn)后兩位或n位的簡(jiǎn)單方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10
IE圖片緩存document.execCommand("BackgroundImageCache",
IE6下設(shè)置背景圖片是不會(huì)被真正cache住的,就算服務(wù)器做了cache,如果想cache住只能2011-03-03
JS根據(jù)瀏覽器窗口大小實(shí)時(shí)動(dòng)態(tài)改變網(wǎng)頁(yè)文字大小的方法
這篇文章主要介紹了JS根據(jù)瀏覽器窗口大小實(shí)時(shí)動(dòng)態(tài)改變網(wǎng)頁(yè)文字大小的方法,涉及JavaScript針對(duì)頁(yè)面寬高的動(dòng)態(tài)獲取與元素樣式動(dòng)態(tài)運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2016-02-02

