公司写项目用ajax吗,项目中经常使用的ajax写法分享

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 = "%5C%22%22+IMAGE_PATH+%22%5C%22";

$("#phonemodelimg").html(imgIcon);

}

if(MODEL_ALIS){

$("#phonemodelalis").html(MODEL_ALIS);

}

});

}

}

});

}

}

/*AjaxTestAction.java*/

//获得手机信息

public String getPhoneInfoByModelId(){

String model_id = this.getHttpRequest().getParameter("model_id");

List> brandList = null;

if(null != model_id && model_id.length() > 0){

brandList = this.phoneAssessService.getPhoneInfoByModelId(model_id);

for(int i = 0,len=brandList.size();i

formatToJsonHashMap(brandList.get(i));

}

JSONArray jsArray = JSONArray.fromObject(brandList);

this.setTextAjax(jsArray.toString());

}

return NONE;

}

//公共方法:格式化空值和日期

public void formatToJsonHashMap(Map 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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值