$.getJSON 的用法:
//判断数据库一条记录中是否存在相同的planCode,cityCode,useType记录
function checkPlanConfig(planCode,useType){
$.getJSON("getPlanConfigRecord.do",{"planCode":planCode,"cityCode":cityCode,"useType":useType},function(datas){
for(var i = 0;i<datas.length;i++){
if((planCode == datas[i].planCode) && (cityCode == datas[i].cityCode) && (useType == datas[i].useType)){
alert("存在相同的险种、城市和车辆使用性质,请重新选择");
document.getElementById("planCode").value="";
document.getElementById("planCode").focus();
document.getElementById("cityCode").value="";
document.getElementById("useType").value="";
return;
}
}
});
}
---------------- /** * 查看数据库中是否存在相同的planCode、cityCode和useType记录 * @param request * @param response * @return * @throws Exception */ @SuppressWarnings("unchecked") protected ModelAndView getPlanConfigRecord(HttpServletRequest request,HttpServletResponse response) throws Exception { Map<String,Object> params = new HashMap<String,Object>(); params.put(Constants.PARAMETER_ACTION,"record"); List<PlanConfig> list = (List<PlanConfig>)dispatchRequest(params,ServiceRequestID.PLAN_CONFIG_ACTION); JsonResponseUtil.writeJsonArray(list,response); return null; } ----------------------- /** * 将Array封装到Json中返回页面 * @param objectArray String * @param response HttpServletResponse */ public static void writeJsonArray(Object objectArray,HttpServletResponse response) { String jsonString = JsonUtil.toJsonArrayString(objectArray); singleInstance.responseJsonString(jsonString,response); } ----------------------- /* * Copyright (c) 2008-2010. * * This software is ...... */ package com.tpaic.ec.web.util.json; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.util.JSONUtils; /** * This JsonUtil is a UTIL tool class for all the AJAX request. * * @Id JsonUtil.java * @version * @author Liuyc ; * @Date Mar 27,2009 */ public class JsonUtil { /** singleInstance */ private static JsonUtil singleInstance; /** * Convert the string data to JSON style. * * @param result * @return */ public static String toJsonString(AjaxResult result) { return singleInstance.generateJsonString(result); } /** * Convert the data object to JSON style. * * @param data * @return */ public static String toJsonObjectString(Object data) { return singleInstance.generateJsonObject(data); } /** * Wrapped the array object to JSON style. * * * @param arrayObject * @return */ public static String toJsonArrayString(Object arrayObject) { return singleInstance.generateJsonArray(arrayObject); } /** JsonConfig contains others processors which want to affect on the JSON string.*/ private JsonConfig jsonConfig = new JsonConfig(); /** * @param result * @return */ public String generateJsonString(AjaxResult result) { switch(result.getRequestType()) { case AjaxResult.AJAX_SUBMIT: return JSONObject.fromObject(result,jsonConfig).toString(); case AjaxResult.AJAX_DATA: if(JSONUtils.isArray(result.getData())) { return JSONArray.fromObject(result.getData(),jsonConfig).toString(); } return JSONObject.fromObject(result.getData(),jsonConfig).toString(); default: throw new RuntimeException(result.toString()); } } /** * @param data * @return */ public String generateJsonObject(Object data) { return JSONObject.fromObject(data,jsonConfig).toString(); } /** * @param * @return */ public String generateJsonArray(Object arrayObject) { return JSONArray.fromObject(arrayObject,jsonConfig).toString(); } public void init() throws Exception { if(JsonUtil.singleInstance == null) { JsonUtil.singleInstance = this; } } } ------------------- /* * Copyright (c) 2008-2010. * * This software is ...... */ package com.tpaic.ec.web.util.json; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; /** * Wrapped the JSON data to response. * * @Id JsonResponseUtil.java * @version * @author Liuyc ; * @Date Mar 27,2009 */ public class JsonResponseUtil { /** logger */ private static Logger logger = Logger.getLogger(JsonResponseUtil.class); /** singleInstance */ private static JsonResponseUtil singleInstance; /** contentType */ private String contentType; /** charsetName */ private String charsetName; /** jsonUtil */ private JsonUtil jsonUtil; /** * constructor. */ public JsonResponseUtil() { if (singleInstance == null) { singleInstance = this; } } /** * * @return JsonResponseUtil */ public static JsonResponseUtil getInstance() { return singleInstance; } /** * 将String封装到Json中返回页面 * @param jsonString String * @param response HttpServletResponse */ public static void writeJsonString(String jsonString,HttpServletResponse response) { singleInstance.responseJsonString(jsonString,response); } /** * 将对象封装到Json中返回页面 * @param data String * @param response HttpServletResponse */ public static void writeJsonObject(Object data,HttpServletResponse response) { String jsonString = JsonUtil.toJsonObjectString(data); singleInstance.responseJsonString(jsonString,response); } /** * 将Array封装到Json中返回页面 * @param objectArray String * @param response HttpServletResponse */ public static void writeJsonArray(Object objectArray,response); } /** * * @param ajaxResult String * @param response HttpServletResponse */ public static void writeAjaxResult(AjaxResult ajaxResult,HttpServletResponse response) { String jsonString = JsonUtil.toJsonString(ajaxResult); singleInstance.responseJsonString(jsonString,response); } /** * @return the contentType */ public String getContentType() { return contentType; } /** * @param contentType * the contentType to set */ public void setContentType(String contentType) { this.contentType = contentType; } /** * @return the jsonUtil */ public JsonUtil getJsonUtil() { return jsonUtil; } /** * @param jsonUtil * the jsonUtil to set */ public void setJsonUtil(JsonUtil jsonUtil) { this.jsonUtil = jsonUtil; } /** * Directly Send the JSON string to client. * * @param jsonString */ public void responseJsonString(String jsonString,HttpServletResponse response) { responseJsonString(response,jsonString); } /** * * @param data Object * @param response HttpServletResponse */ public void responseJsonObject(Object data,HttpServletResponse response) { String jsonString = jsonUtil.generateJsonObject(data); responseJsonString(jsonString,response); } /** * * @param objectArray Object * @param response HttpServletResponse */ public void responseJsonArray(Object objectArray,HttpServletResponse response) { String jsonString = jsonUtil.generateJsonArray(objectArray); responseJsonString(jsonString,response); } /** * * @param ajaxResult String * @param response HttpServletResponse */ public void responseAjaxResult(AjaxResult ajaxResult,HttpServletResponse response) { String jsonString = jsonUtil.generateJsonString(ajaxResult); responseJsonString(jsonString,response); } /** * * @param charsetName */ public void setCharsetName(String charsetName) { this.charsetName = charsetName; } /** * 初始化Json * @throws Exception Exception */ public void init() throws Exception { if (StringUtils.isBlank(contentType) || StringUtils.isBlank(charsetName)) { throw new Exception("Both of contentType and charsetName are requied."); } } /** * Directly Send the JSON string to client. * * @param response * @param jsonString */ public void responseJsonString(HttpServletResponse response,String jsonString) { response.setContentType(contentType); byte[] bytes; try { bytes = jsonString.getBytes(charsetName); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } response.setContentLength(bytes.length); try { OutputStream os = response.getOutputStream(); os.write(bytes); os.flush(); if (logger.isDebugEnabled()) { logger.debug("Send the client" + jsonString); } } catch (IOException e) { logger.error("Exception happens.",e); throw new RuntimeException(e); } } }