加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax的例子

发布时间:2020-12-16 01:41:46 所属栏目:百科 来源:网络整理
导读:例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" }, s

例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("数据有误"); } }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读