<script> function GetJsonObj(str) { return JSON.parse ? JSON.parse(str) : eval('(' + str + ')'); } var Ajax = function() { if (typeof(XMLHttpRequest) === "undefined") { XMLHttpRequest = function() { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { throw new Error("This browser does not support XMLHttpRequest."); } }; } return new XMLHttpRequest(); }(); function ToAjax(url, callback) { Ajax.onreadystatechange = function() { if(Ajax.readyState == 4 && Ajax.status == 200) { callback(GetJsonObj(Ajax.responseText)); } }; Ajax.open("GET", url, false); //false=同步 Ajax.send(); } function click() { ToAjax("http://localhost:8080/web1?A=B&B=C", function(json){ document.getElementById("div2").innerHTML = json.Data; }); } </script> //////////////// <script> function GetJsonObj(str) { return JSON.parse ? JSON.parse(str) : eval('(' + str + ')'); } function GetXmlHttpObject() { if (typeof(XMLHttpRequest) === "undefined") { XMLHttpRequest = function() { try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { throw new Error("This browser does not support XMLHttpRequest."); } }; } return new XMLHttpRequest(); } var Ajax = GetXmlHttpObject(); function click() { Ajax.onreadystatechange = function() { if(Ajax.readyState == 4 && Ajax.status == 200){ var str = Ajax.responseText; var Json = GetJsonObj(str); str = Json.Data; document.getElementById("div2").innerHTML = str; } }; Ajax.open("POST", "http://localhost:8080/web1", true); Ajax.send('A=B&B=C'); } </script> //LoadScript function loadScript(url, callback){ var Script = document.createElement("script") ; Script.type = "text/javascript"; Script.id = 'jsd'; if (Script.readyState){ //IE Script.onreadystatechange = function(){ if (Script.readyState == "loaded" || Script.readyState == "complete") { Script.onreadystatechange = null; callback(); } }; } else { Script.onload = function(){ callback(); }; } Script.src = url; document.body.appendChild(Script); } function click() { loadScript("js/test2.js", function(){ alert(xxx); }); }
posted on 2015-11-06 12:28  万一  阅读(176)  评论(0)    收藏  举报