ajax的例子
例1 前端: var setlocation=function(mapx,mapy,address){ 后台: public void savelocation() {
————————————————————————————————————————————————————————————————————————————— 例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("数据有误"); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |