how2j.cn


工具版本兼容问题
通过AJAX Asynchronous JAvaScript and XML 实现异步刷新

示例 1 : 用于用户名校验的页面   
示例 2 : 不使用AJAX 通过刷新页面验证账号是否存在   
示例 3 : 使用AJAX 通过无刷新验证账号是否存在   

示例 1 :

用于用户名校验的页面

准备一个JSP页面,叫做checkName.jsp用于校验提交的用户名是否存在
如果提交的用户名是abc就打印存在,否则就可以使用


提示:使用F5回到原来页面
"); window.frames["iframe_show1230"].document.write(decodeHtml($("textarea#stepcodeTextarea1230").val())); window.frames["iframe_show1230"].document.close(); $(window.frames["iframe_show1230"]).load(function(){ $("#iframe_show1230").height($("#iframe_show1230").contents().find("body").height()+showittryitheight); }); $("#iframe_show1230").height($("#iframe_show1230").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<html> <a href="http://127.0.0.1/study/checkName.jsp?name=abc">checkName.jsp?name=abc</a> <br> <a href="http://127.0.0.1/study/checkName.jsp?name=def">checkName.jsp?name=def</a> </html>
<html>

<a href="http://127.0.0.1/study/checkName.jsp?name=abc">checkName.jsp?name=abc</a>
<br>
<a href="http://127.0.0.1/study/checkName.jsp?name=def">checkName.jsp?name=def</a>

</html>

"); window.frames["iframe1230"].document.write(decodeHtml(code1230)); window.frames["iframe1230"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1230"]).load(function(){ $("#iframe1230").height($("#iframe1230").contents().find("body").height()+showittryitheight); }); $("#iframe1230").height($("#iframe1230").contents().find("body").height()+showittryitheight); alreadyWriteCode1230 = code1230; $("#rendering1230").hide(); $("#rendered1230").show(); } var tRereshRetry2DemoPanel1230 = setInterval(rereshRetry2DemoPanel1230,1000); var binded1230 = false; $("textarea#stepcodeTextarea1230").keyup(function(){ if(!binded1230){ $(window).bind('beforeunload',function(){ binded1230 = true; return "xxxx"; }); } var newCode = $(this).val() code1230 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1230!=newCode){ // window.frames["iframe1230"].document.write("
"); // window.frames["iframe1230"].document.write(decodeHtml($("textarea#stepcodeTextarea1230").val())); // window.frames["iframe1230"].document.close(); // $(window.frames["iframe1230"]).load(function(){ // $("#iframe1230").height($("#iframe1230").contents().find("body").height()+showittryitheight); // }); // code1230 = newCode; // } }); $(".tryButton1230").click(function(){ $("#tryDiv1230").show(); $("#stepcodeTextarea1230").focus(); $("#stepcodeTextarea1230").height(200); $("#iframe1230").height(0); window.frames["iframe1230"].document.write("
"); window.frames["iframe1230"].document.write(decodeHtml($("textarea#stepcodeTextarea1230").val())); window.frames["iframe1230"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1230"]).load(function(){ $("#iframe1230").height($("#iframe1230").contents().find("body").height()+showittryitheight); }); $("#iframe1230").height($("#iframe1230").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1230.focus(); editor1230.setSize(null, "250"); $("#rendering1230").hide(); $("#rendered1230").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1230 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1230"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1230.on("change",function(doc){ if(!binded1230){ $(window).bind('beforeunload',function(){ binded1230 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1230 = newCode; $("textarea#stepcodeTextarea1230").val(newCode); if(alreadyWriteCode1230!=code1230){ lastModifedTime1230 = new Date().getTime(); $("#rendering1230").show(); $("#rendered1230").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1230 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1230.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1230").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 2 :

不使用AJAX 通过刷新页面验证账号是否存在

如果不使用AJAX,传统的方法需要通过提交数据 刷新页面来获知用户名是否存在。
提示:使用F5回到原来页面
"); window.frames["iframe_show1228"].document.write(decodeHtml($("textarea#stepcodeTextarea1228").val())); window.frames["iframe_show1228"].document.close(); $(window.frames["iframe_show1228"]).load(function(){ $("#iframe_show1228").height($("#iframe_show1228").contents().find("body").height()+showittryitheight); }); $("#iframe_show1228").height($("#iframe_show1228").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<form action="http://127.0.0.1/study/checkName.jsp"> 输入账号 <input name="name" type="text" value="abc"> <input type="submit" value="验证账号是否存在"> </form>
<form action="http://127.0.0.1/study/checkName.jsp">

输入账号 <input name="name" type="text" value="abc"> 

<input type="submit" value="验证账号是否存在">

</form>
"); window.frames["iframe1228"].document.write(decodeHtml(code1228)); window.frames["iframe1228"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1228"]).load(function(){ $("#iframe1228").height($("#iframe1228").contents().find("body").height()+showittryitheight); }); $("#iframe1228").height($("#iframe1228").contents().find("body").height()+showittryitheight); alreadyWriteCode1228 = code1228; $("#rendering1228").hide(); $("#rendered1228").show(); } var tRereshRetry2DemoPanel1228 = setInterval(rereshRetry2DemoPanel1228,1000); var binded1228 = false; $("textarea#stepcodeTextarea1228").keyup(function(){ if(!binded1228){ $(window).bind('beforeunload',function(){ binded1228 = true; return "xxxx"; }); } var newCode = $(this).val() code1228 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1228!=newCode){ // window.frames["iframe1228"].document.write("
"); // window.frames["iframe1228"].document.write(decodeHtml($("textarea#stepcodeTextarea1228").val())); // window.frames["iframe1228"].document.close(); // $(window.frames["iframe1228"]).load(function(){ // $("#iframe1228").height($("#iframe1228").contents().find("body").height()+showittryitheight); // }); // code1228 = newCode; // } }); $(".tryButton1228").click(function(){ $("#tryDiv1228").show(); $("#stepcodeTextarea1228").focus(); $("#stepcodeTextarea1228").height(200); $("#iframe1228").height(0); window.frames["iframe1228"].document.write("
"); window.frames["iframe1228"].document.write(decodeHtml($("textarea#stepcodeTextarea1228").val())); window.frames["iframe1228"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1228"]).load(function(){ $("#iframe1228").height($("#iframe1228").contents().find("body").height()+showittryitheight); }); $("#iframe1228").height($("#iframe1228").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1228.focus(); editor1228.setSize(null, "250"); $("#rendering1228").hide(); $("#rendered1228").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1228 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1228"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1228.on("change",function(doc){ if(!binded1228){ $(window).bind('beforeunload',function(){ binded1228 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1228 = newCode; $("textarea#stepcodeTextarea1228").val(newCode); if(alreadyWriteCode1228!=code1228){ lastModifedTime1228 = new Date().getTime(); $("#rendering1228").show(); $("#rendered1228").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1228 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1228.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1228").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果
示例 3 :

使用AJAX 通过无刷新验证账号是否存在

"); window.frames["iframe_show1229"].document.write(decodeHtml($("textarea#stepcodeTextarea1229").val())); window.frames["iframe_show1229"].document.close(); $(window.frames["iframe_show1229"]).load(function(){ $("#iframe_show1229").height($("#iframe_show1229").contents().find("body").height()+showittryitheight); }); $("#iframe_show1229").height($("#iframe_show1229").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<span>输入账号 :</span> <input id="name" name="name" onkeyup="check()" type="text"> <span id="checkResult"></span> <script> var xmlhttp; function check(){ var name = document.getElementById("name").value; var url = "http://127.0.0.1/study/checkName.jsp?name="+name; xmlhttp =new XMLHttpRequest(); xmlhttp.onreadystatechange=checkResult; //响应函数 xmlhttp.open("GET",url,true); //设置访问的页面 xmlhttp.send(null); //执行访问 } function checkResult(){ if (xmlhttp.readyState==4 && xmlhttp.status==200) document.getElementById('checkResult').innerHTML=xmlhttp.responseText; } </script>
"); window.frames["iframe1229"].document.write(decodeHtml(code1229)); window.frames["iframe1229"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1229"]).load(function(){ $("#iframe1229").height($("#iframe1229").contents().find("body").height()+showittryitheight); }); $("#iframe1229").height($("#iframe1229").contents().find("body").height()+showittryitheight); alreadyWriteCode1229 = code1229; $("#rendering1229").hide(); $("#rendered1229").show(); } var tRereshRetry2DemoPanel1229 = setInterval(rereshRetry2DemoPanel1229,1000); var binded1229 = false; $("textarea#stepcodeTextarea1229").keyup(function(){ if(!binded1229){ $(window).bind('beforeunload',function(){ binded1229 = true; return "xxxx"; }); } var newCode = $(this).val() code1229 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1229!=newCode){ // window.frames["iframe1229"].document.write("
"); // window.frames["iframe1229"].document.write(decodeHtml($("textarea#stepcodeTextarea1229").val())); // window.frames["iframe1229"].document.close(); // $(window.frames["iframe1229"]).load(function(){ // $("#iframe1229").height($("#iframe1229").contents().find("body").height()+showittryitheight); // }); // code1229 = newCode; // } }); $(".tryButton1229").click(function(){ $("#tryDiv1229").show(); $("#stepcodeTextarea1229").focus(); $("#stepcodeTextarea1229").height(200); $("#iframe1229").height(0); window.frames["iframe1229"].document.write("
"); window.frames["iframe1229"].document.write(decodeHtml($("textarea#stepcodeTextarea1229").val())); window.frames["iframe1229"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1229"]).load(function(){ $("#iframe1229").height($("#iframe1229").contents().find("body").height()+showittryitheight); }); $("#iframe1229").height($("#iframe1229").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1229.focus(); editor1229.setSize(null, "250"); $("#rendering1229").hide(); $("#rendered1229").hide(); }); var mixedMode = { name: "htmlmixed", scriptTypes: [{matches: /\/x-handlebars-template|\/x-mustache/i, mode: null}, {matches: /(text|application)\/(x-)?vb(a|script)/i, mode: "vbscript"}] }; var editor1229 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1229"), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, mode:"text/html", theme:"eclipse", selectionPointer: true, lineWrapping: true, extraKeys: { "Alt-/": "autocomplete", "Ctrl-F": "findPersistent", "F8": function(cm) { cm.setOption("fullScreen", !cm.getOption("fullScreen")); }, "Esc": function(cm) { if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false); } } }); editor1229.on("change",function(doc){ if(!binded1229){ $(window).bind('beforeunload',function(){ binded1229 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1229 = newCode; $("textarea#stepcodeTextarea1229").val(newCode); if(alreadyWriteCode1229!=code1229){ lastModifedTime1229 = new Date().getTime(); $("#rendering1229").show(); $("#rendered1229").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1229 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1229.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1229").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


源代码
1. 双击选中单词 2. 三击选中整行 3. CTRL+F 查找 4. F8 全屏编辑,再次点击恢复
渲染中 渲染完成
效果


HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。


关于 前端部分-Ajax-Hello AJAX 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 620943819
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: http://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图