日期:2014-05-16 浏览次数:20703 次
ajax函数(同步方式): function queryIsSameLevel(){ var ID_MT_OPERATIONTYPE = document.getElementById('ID_MT_OPERATIONTYPE').value; var url = '<%=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() %>' var executeURL = url+"/common/mt/isSameLevel.do?id="+ID_MT_OPERATIONTYPE; var xmlhttp; if (window.XMLHttpRequest){ xmlhttp= new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //同步方式 xmlhttp.open("POST",executeURL,false); xmlhttp.send(); if(xmlhttp.readyState==4 && xmlhttp.status==200){ //alert("你大爷" + xmlhttp.responseText); responseTextVal = xmlhttp.responseText; responseTextVal = responseTextVal.replace('{',''); responseTextVal = responseTextVal.replace('}',''); var index = responseTextVal.indexOf(":"); responseTextVal = responseTextVal.substring(index+1); responseTextVal = responseTextVal.replace('"',''); responseTextVal = responseTextVal.replace('"',''); } } 异步方式: function queryIsSameLevel(){ var ID_MT_OPERATIONTYPE = document.getElementById('ID_MT_OPERATIONTYPE').value; var url = '<%=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() %>' var executeURL = url+"/common/mt/isSameLevel.do?id="+ID_MT_OPERATIONTYPE; var xmlhttp; if (window.XMLHttpRequest){ xmlhttp= new XMLHttpRequest(); }else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //异步方式 xmlhttp.open("POST",executeURL,true); xmlhttp.send(); //异步方式添加回调函数 xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4 && xmlhttp.status==200){ //alert("你大爷" + xmlhttp.responseText); responseTextVal = xmlhttp.responseText; responseTextVal = responseTextVal.replace('{',''); responseTextVal = responseTextVal.replace('}',''); var index = responseTextVal.indexOf(":"); responseTextVal = responseTextVal.substring(index+1); responseTextVal = responseTextVal.replace('"',''); responseTextVal = responseTextVal.replace('"',''); } } } 如果有一个方法B()调用上述两个方法queryIsSameLevel(), 异步方式:有可能回调函数的执行比B函数中的代码执行要晚 同步的方式:等方法queryIsSameLevel()全部执行完后再去执行方法B()中的代码 同步方式没有回调函数,执行完send()方法后就进行状态的判断 参考资料: http://hi.baidu.com/iamgongjunhao/blog/item/e4357a501ae429748535242f.html