项目中经常使用的ajax写法分享

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

function getPhoneInfoByModelId(model_id){
	if(model_id){
		$.ajax({
	        url: "${applicationScope.rootpath}phoneassess/getPhoneInfoByModelId.action",
	        type: "post",
	        dataType:"json",
	        data: {model_id:model_id},
	        success: function (jsonText) {
	        	if(jsonText){
	        		$.each(jsonText,function(i,item){
	        			var IMAGE_PATH = item.IMAGE_PATH;
		        		var MODEL_ALIS = item.MODEL_ALIS;
		        		//alert(IMAGE_PATH+"--"+MODEL_ALIS);
		        		if(IMAGE_PATH){
		        			var imgIcon = "<img src=\""+IMAGE_PATH+"\"/>";
		        			$("#phonemodelimg").html(imgIcon);
		        		}
		        		if(MODEL_ALIS){
		        			$("#phonemodelalis").html(MODEL_ALIS);
		        		}
	        		});
		        }
	        }
     	});
	}
}



/*AjaxTestAction.java*/
//获得手机信息
	public String getPhoneInfoByModelId(){
		String model_id = this.getHttpRequest().getParameter("model_id");
		List<Map<String, Object>> brandList = null;
		if(null != model_id && model_id.length() > 0){
			brandList = this.phoneAssessService.getPhoneInfoByModelId(model_id);
			for(int i = 0,len=brandList.size();i<len;i++){
				formatToJsonHashMap(brandList.get(i));
			}
			JSONArray jsArray = JSONArray.fromObject(brandList);
			this.setTextAjax(jsArray.toString());
		}
		return NONE;
	}
//公共方法:格式化空值和日期
	public void formatToJsonHashMap(Map<String,Object> hashMap){
		Iterator iter = hashMap.entrySet().iterator();
		while (iter.hasNext()) {
		   Map.Entry entry = (Map.Entry) iter.next();
		   String key = entry.getKey().toString();
		   if(entry.getValue()!=null&&"java.sql.Date".equals(entry.getValue().getClass().getName())){
			   hashMap.put(key, DateUtil.format((Date)entry.getValue(), "yyyy-MM-dd"));
		   }
		}
	}
/**
	 * ajax返回html,包括json形式
	 * 
	 * @param responseContent
	 */
	public void setTextAjax(String responseContent) {
		try {
			HttpServletResponse response = getHttpResponse();
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Content-Type", "text/html");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			PrintWriter out = response.getWriter();
			out.print(responseContent);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// ajaxResponse = new StringBufferInputStream(responseContent);
	}