异步实例

HTML的<head>标签中的JS,

 

例子:实现异步

 

<!--

<script type="text/javascript">

function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
var center = new GLatLng(39.9493, 116.3975);
map.setCenter(center, 13);

var marker = new GMarker(center, {draggable: true});


GEvent.addListener(marker, "dragstart", function() {
map.closeInfoWindow();

});

GEvent.addListener(marker, "dragend", function() {
//  alert(marker.getLatLng().lat());
//    alert(marker.getLatLng().lng());
marker.openInfoWindowHtml("弹起来了...");
document.getElementById('lat').value= marker.getLatLng().lat();
document.getElementById('lng').value= marker.getLatLng().lng();


});

map.addOverlay(marker);

}
}

</script>
<script type="text/javascript">
var Try = {
these: function() {
var returnValue;

for (var i = 0, length = arguments.length; i < length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch (e) { }
}

return returnValue;
}
};

var Ajax = {

/**
* Creates a new XMLHttpRequest object.
* @alias Ajax.getTransport
* @return {XMLHttpRequest} Returns a new XMLHttpRequest object.
*/
getTransport: function() {
return Try.these(
function() {return new XMLHttpRequest()},
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')}
) || false;
},

activeRequestCount: 0
};




function testSaverMarker(){

var lat = document.getElementById("lat").value;
var lng = document.getElementById("lng").value;
var request = Ajax.getTransport();
request.open("post","/mapserver/savemarker?lat="+lat+"&lng="+lng,true);

request.onreadystatechange = function(){
if(request.readyState == 4){//约定只要有了返回结果,无论何时都可以alert
alert(request.responseText);
var obj = eval('('+request.responseText + ')');
alert(obj.success);
alert(obj.lat);
alert(obj.lng);
document.getElementById('result').innerHTML=request.responseText;
}  
}  
request.send(null);//我真的要发送到后台了
//  document.getElementById('result').innerHTML=request.responseText;
return false;
}





</script>

 

-->

HTML的<body>标签中的代码

 

<!--

 

<body οnlοad="initialize()" οnunlοad="GUnload()" >
<div id="map_canvas" style="width: 500px; height: 300px"></div>

lat: <input type="text" name="lat" id="lat" /> <br/><br/>
lng: <input type="text" name="lng" id="lng" /><br/><br/>
<input name="submit" type="submit" value="OK" οnclick="testSaverMarker()";/>
<div id="result"></div>
</body>

 

-->

 

servlet中post方法中的代码回写,其中还用到json格式:

<!--

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("now you are in post..");

String ip=request.getRemoteAddr();

System.out.println("Now the ip is "+ip);

String llq=request.getHeader("user-agent");

System.out.println("Now the user-agent is "+llq);



String thisURI=request.getRequestURI();

System.out.println("Now the URI is "+thisURI);   
if(thisURI.equals("/mapserver/savemarker")){
String lat = request.getParameter("lat");
String lng = request.getParameter("lng");

System.out.println("lat="+lat);
System.out.println("lng="+lng);
response.setCharacterEncoding("utf-8");

//response.getWriter().write("保存成功"+lat+":"+lng);
String jsonstr = "{\"success\":true,\"lat\":"+lat+",\"lng\":"+lng+"}";
Map map = new HashMap();
map.put("success", true);
map.put("lat", lat);
map.put("lng", lng);
String jsonText = JSONValue.toJSONString(map);
response.getWriter().write(jsonText);
}

-->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值