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注冊監(jiān)聽事件和清除監(jiān)聽事件方式詳解
js中事件監(jiān)聽就是利用addEventListener來綁定一個事件,這個用法在jquery中非常常用并且簡單,但在原生js中比較復(fù)雜,下面這篇文章主要給大家介紹了關(guān)于JavaScript注冊監(jiān)聽事件和清除監(jiān)聽事件方式的相關(guān)資料,需要的朋友可以參考下2023-05-05重學(xué) JS:為啥 await 不能用在 forEach 中詳解
這篇文章主要介紹了重學(xué) JS:為啥 await 不能用在 forEach 中,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04關(guān)于Vue中postcss-pxtorem的使用詳解
在Web開發(fā)領(lǐng)域,響應(yīng)式設(shè)計已經(jīng)成為一個不可或缺的趨勢,PostCSS插件——postcss-pxtorem的出現(xiàn)為我們提供了一種更加智能和高效的解決方案,本文將深入探討postcss-pxtorem的使用,包括其背后的原理、配置選項、實際應(yīng)用中的注意事項等方面,需要的朋友可以參考下2023-12-12對存在JavaScript隱式類型轉(zhuǎn)換的四種情況的總結(jié)(必看篇)
下面小編就為大家?guī)硪黄獙Υ嬖贘avaScript隱式類型轉(zhuǎn)換的四種情況的總結(jié)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08