ajax的例子

前端之家收集整理的这篇文章主要介绍了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("数据有误"); } }

原文链接:https://www.f2er.com/ajax/162337.html

猜你在找的Ajax相关文章