ajax的例子

例1

前端:

var setlocation=function(mapx,mapy,address){
$.ajax({
url: "${ctx}/crm/customer.action?method:savelocation",
type:"post",
dataType:"json",
data:{
customerId:id,
mapx:mapx,
mapy:mapy,
attr:address,
"method:savelocation":"xx"
},
success : function(data) {
var json = eval(data);
     var flag = json.flag;
if(flag == "true"){
alert("保存成功!");
var locationhref="${ ctx}/crm/customer.action?method:editForm&customer.id=${ customer.id }&pageNo=${ param.pageNo }&pageSize=${ param.pageSize }";
var centerinfo = new window.BMap.InfoWindow(
"<p style=’font-size:12px;lineheight:1.8em;’>"
+"公司:"+"<a href="+locationhref+">${customer.name}</a>"+"</br> "
+ "x:"+ mapx + "</br> "
+"y:"+ mapy + "</br>"
+ "地址:"+ newaddress + "</br> "
+ "</br></p>");
    marker.addEventListener("mouseover", function() {
this.openInfoWindow(centerinfo);
});

}
else
alert("保存失败!");
}
        });
}

后台:

public void savelocation() {
String flag = "false";
try{
String id=getRequest().getParameter("customerId");
String mapx=getRequest().getParameter("mapx");
String mapy=getRequest().getParameter("mapy");
System.out.println("mapxy:"+mapx+","+mapy);
double[] mapxy={Double.parseDouble(mapx),Double.parseDouble(mapy)};
double[] lonlat=AppUtils.mapXYTolonlat(mapxy);
String lontitude=lonlat[0]+"";
String latitude=lonlat[1]+"";
System.out.println("lonlat:"+lontitude+","+latitude);
String attr=getRequest().getParameter("attr");
Customer customer =customerManager.get(Long.parseLong(id));
customer.setLatitude(latitude);
customer.setLontitude(lontitude);
customer.setAddress(attr);
customerManager.save(customer);

flag = "true";
}catch(Exception e){
e.printStackTrace();
}
JSONObject json = new JSONObject();
json.put("flag", flag);
responseWrite(json.toString());
}


—————————————————————————————————————————————————————————————————————————————

例2

前端:

function ajax_init(){

$.ajax({ 
url: '${ctx }/crm/customer.action?method:locationsAsJson',
async: false,
dataType:'json',
        type: "POST",  
        data: {"lontitude":lontitude,"latitude":latitude}, 
success: function(tt){  
if(tt[0].total>0){
for (var i = 0; i < tt[0].total; i++) {
var id=(tt[0].locations)[i].customerid;
var addr=(tt[0].locations)[i].address;
ids.push(id);
addrs.push(addr);
}
}
         }
});

}


后台:

public void locationsAsJson() {
HttpServletRequest request= ServletActionContext.getRequest();

String lontitude=request.getParameter("lontitude");
String latitude=request.getParameter("latitude");


if(!lontitude.equals("/")&&!latitude.equals("/")){


String customerSql = "SELECT f_id,f_address,f_name,f_latitude,f_lontitude,"+
"(POWER(ABS(f_latitude - "+latitude+"),2) + POWER(MOD(ABS(f_lontitude - "+lontitude+"),360),2)) AS distance "+
"FROM t_customer "+
"WHERE f_lontitude IS NOT NULL "+
"AND f_latitude IS NOT NULL "+
"AND f_latitude <> "+latitude+" "+
"AND f_lontitude <> "+lontitude+" "+
"ORDER BY distance LIMIT 10";
List<Map> rs = manager.findByNativeSqlAsMap(customerSql, null);
JSONObject result = new JSONObject();
JSONArray jsonArr = new JSONArray();
for (Map m : rs) {
Long id = ((BigInteger) m.get("f_id")).longValue();
String addr = (String) m.get("f_address");
JSONObject jsonObj = new JSONObject();
jsonObj.put("customerid",id);
jsonObj.put("address", addr);
jsonArr.add(jsonObj);
}
result.put("locations", jsonArr);
result.put("total", rs.size());
System.out.println(result.toString()); 
responseWrite("["+result.toString()+"]");
}else{
System.out.println("数据有误");
}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值