Ajax ------ Json, jqueryajaxjson
Background code:
String result = "";List<DeviceSensor> devices = deviceSensorService.findAll();JSONArray array = new JSONArray();for (DeviceSensor deviceSensor : devices) {try {String apMac = deviceSensor.getApMac();if(apMac == null) continue;DeviceAp deviceAp = deviceApService.findByAddress(apMac);Map<String,Object> map = new HashMap<String, Object>();map.put("apmac", apMac);map.put("apx", deviceAp.getX());map.put("apy", deviceAp.getY());map.put("sensormac", deviceSensor.getMac());map.put("sensorx", deviceSensor.getX());map.put("sensory", deviceSensor.getY());array.add(map);} catch (Exception e) {}}result = array.toString();try {response.setContentType("application/json;charset-utf-8");response.setContentLength(result.length());response.setCharacterEncoding("utf-8");response.getWriter().write(result);response.getWriter().flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
Return Json Data Format
Js Code:
Refresh (); window. markers = []; window. markerList = new Array (); window. label = new Array (); function refresh () {$. ajax ({type: "GET", dataType: "json", url: "$ {ctx}/link/ajax", success: function (data) {var arr = eval (data); for (var I = 0; I <arr. length; I ++) {if (arr [I]. apy = null | arr [I]. retrial = null) continue; var station = new BMap. point (arr [I]. retrial, arr [I]. apy); // create the markerList [I] = new BMap. marker (new BMap. point (arr [I]. sensorx, arr [I]. sensory); // creates a map annotation. addOverlay (markerList [I]); markerList [I]. setTitle (arr [I]. sensormac); markers. push (markerList [I]); var points = [station, new BMap. point (arr [I]. sensorx, arr [I]. sensory)]; var curve = new BMapLib. curveLine (points, {strokeColor: "blue", strokeWeight: 3, strokeOpacity: 0.5}); // create an arc object map. addOverlay (curve); // Add it to the map. enableEditing (); // enable the editing function/* alert ("apmac:" + arr [I]. apmac + "\ n" + "retrial:" + arr [I]. retrial + "\ n" + "apy:" + arr [I]. apy + "\ n" + "sensormac:" + arr [I]. sensormac + "\ n" + "sensorx:" + arr [I]. sensorx + "\ n" + "sensory:" + arr [I]. sensory + "\ n") */} cluster () ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown );}});}