how2j.cn


工具版本兼容问题
分步解析 AJAX 每个步骤

步骤 1 : AJAX 请求和相应图示   
步骤 2 : 创建XHR   
步骤 3 : 设置响应函数   
步骤 4 : 设置并发出请求   
步骤 5 : 处理响应信息   
步骤 6 : 再完整的演示一遍   
步骤 7 : checkName.jsp   

步骤 1 :

AJAX 请求和相应图示

AJAX 请求和相应图示
步骤 2 :

创建XHR

创建XHR对象 XMLHttpRequest
XHR对象是一个javascript对象,它可以在用户没有感觉的情况下,就像背后运行的一根小线程一般,悄悄的和服务器进行数据交互
AJAX就是通过它做到无刷新效果的
"); window.frames["iframe_show963"].document.write(decodeHtml($("textarea#stepcodeTextarea963").val())); window.frames["iframe_show963"].document.close(); $(window.frames["iframe_show963"]).load(function(){ $("#iframe_show963").height($("#iframe_show963").contents().find("body").height()+showittryitheight); }); $("#iframe_show963").height($("#iframe_show963").contents().find("body").height()+showittryitheight); setTimeout(function(){ },500); });
运行效果
<script> var xmlhttp =new XMLHttpRequest(); document.write(xmlhttp); </script>
<script>
var xmlhttp =new XMLHttpRequest(); 
document.write(xmlhttp);
</script>
"); window.frames["iframe963"].document.write(decodeHtml(code963)); window.frames["iframe963"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe963"]).load(function(){ $("#iframe963").height($("#iframe963").contents().find("body").height()+showittryitheight); }); $("#iframe963").height($("#iframe963").contents().find("body").height()+showittryitheight); alreadyWriteCode963 = code963; $("#rendering963").hide(); $("#rendered963").show(); } var tRereshRetry2DemoPanel963 = setInterval(rereshRetry2DemoPanel963,1000); var binded963 = false; $("textarea#stepcodeTextarea963").keyup(function(){ if(!binded963){ $(window).bind('beforeunload',function(){ binded963 = true; return "xxxx"; }); } var newCode = $(this).val() code963 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code963!=newCode){ // window.frames["iframe963"].document.write("
"); // window.frames["iframe963"].document.write(decodeHtml($("textarea#stepcodeTextarea963").val())); // window.frames["iframe963"].document.close(); // $(window.frames["iframe963"]).load(function(){ // $("#iframe963").height($("#iframe963").contents().find("body").height()+showittryitheight); // }); // code963 = newCode; // } }); $(".tryButton963").click(function(){ $("#tryDiv963").show(); $("#stepcodeTextarea963").focus(); $("#stepcodeTextarea963").height(200); $("#iframe963").height(0); window.frames["iframe963"].document.write("
"); window.frames["iframe963"].document.write(decodeHtml($("textarea#stepcodeTextarea963").val())); window.frames["iframe963"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe963"]).load(function(){ $("#iframe963").height($("#iframe963").contents().find("body").height()+showittryitheight); }); $("#iframe963").height($("#iframe963").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor963.focus(); editor963.setSize(null, "250"); $("#rendering963").hide(); $("#rendered963").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 editor963 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea963"), { 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); } } }); editor963.on("change",function(doc){ if(!binded963){ $(window).bind('beforeunload',function(){ binded963 = true; return "xxxx"; }); } var newCode = doc.getValue(); code963 = newCode; $("textarea#stepcodeTextarea963").val(newCode); if(alreadyWriteCode963!=code963){ lastModifedTime963 = new Date().getTime(); $("#rendering963").show(); $("#rendered963").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor963 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor963.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv963").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

设置响应函数

XHR对象的作用是和服务器进行交互,所以既会发消息给服务器,也能接受服务器返回的响应。
当服务器响应回来的时候,调用怎么处理呢?
通过 xmlhttp.onreadystatechange=checkResult 就可以指定用checkResult 函数进行处理。
步骤 4 :

设置并发出请求

通过open函数设置背后的这个小线程,将要访问的页面url ,在本例中就是
/study/checkName.jsp

xmlhttp.open("GET",url,true);

通过send函数进行实际的访问

xmlhttp.send(null);


null表示没有参数,因为参数已经通过“GET" 方式,放在url里了。
只有在用"POST",并且需要发送参数的时候,才会使用到send。
类似这样:
xmlhttp.send("user="+username+"&password="+password)
步骤 5 :

处理响应信息

在checkResult 函数中处理响应

function checkResult(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
document.getElementById('checkResult').innerHTML=xmlhttp.responseText;
}


xmlhttp.readyState 4 表示请求已完成
xmlhttp.status 200 表示响应成功
xmlhttp.responseText; 用于获取服务端传回来的文本
document.getElementById('checkResult').innerHTML 设置span的内容为服务端传递回来的文本
处理响应信息
步骤 6 :

再完整的演示一遍

1. 创建XHR对象
2. 设置响应函数
3. 设置要访问的页面
4. 发出请求
5. 当服务端的响应返回,响应函数被调用。
6. 在响应函数中,判断响应是否成功,如果成功获取服务端返回的文本,并显示在span中。
"); window.frames["iframe_show1231"].document.write(decodeHtml($("textarea#stepcodeTextarea1231").val())); window.frames["iframe_show1231"].document.close(); $(window.frames["iframe_show1231"]).load(function(){ $("#iframe_show1231").height($("#iframe_show1231").contents().find("body").height()+showittryitheight); }); $("#iframe_show1231").height($("#iframe_show1231").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["iframe1231"].document.write(decodeHtml(code1231)); window.frames["iframe1231"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1231"]).load(function(){ $("#iframe1231").height($("#iframe1231").contents().find("body").height()+showittryitheight); }); $("#iframe1231").height($("#iframe1231").contents().find("body").height()+showittryitheight); alreadyWriteCode1231 = code1231; $("#rendering1231").hide(); $("#rendered1231").show(); } var tRereshRetry2DemoPanel1231 = setInterval(rereshRetry2DemoPanel1231,1000); var binded1231 = false; $("textarea#stepcodeTextarea1231").keyup(function(){ if(!binded1231){ $(window).bind('beforeunload',function(){ binded1231 = true; return "xxxx"; }); } var newCode = $(this).val() code1231 = newCode; /*below code is replaced by function rereshRetry2DemoPanel()*/ // if(code1231!=newCode){ // window.frames["iframe1231"].document.write("
"); // window.frames["iframe1231"].document.write(decodeHtml($("textarea#stepcodeTextarea1231").val())); // window.frames["iframe1231"].document.close(); // $(window.frames["iframe1231"]).load(function(){ // $("#iframe1231").height($("#iframe1231").contents().find("body").height()+showittryitheight); // }); // code1231 = newCode; // } }); $(".tryButton1231").click(function(){ $("#tryDiv1231").show(); $("#stepcodeTextarea1231").focus(); $("#stepcodeTextarea1231").height(200); $("#iframe1231").height(0); window.frames["iframe1231"].document.write("
"); window.frames["iframe1231"].document.write(decodeHtml($("textarea#stepcodeTextarea1231").val())); window.frames["iframe1231"].document.close(); //load和下面的非load必需并存,因为如果代码用到了jquery就必须使用load的方式 $(window.frames["iframe1231"]).load(function(){ $("#iframe1231").height($("#iframe1231").contents().find("body").height()+showittryitheight); }); $("#iframe1231").height($("#iframe1231").contents().find("body").height()+showittryitheight); this.scrollIntoView(true); editor1231.focus(); editor1231.setSize(null, "250"); $("#rendering1231").hide(); $("#rendered1231").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 editor1231 = CodeMirror.fromTextArea(document.getElementById("stepcodeTextarea1231"), { 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); } } }); editor1231.on("change",function(doc){ if(!binded1231){ $(window).bind('beforeunload',function(){ binded1231 = true; return "xxxx"; }); } var newCode = doc.getValue(); code1231 = newCode; $("textarea#stepcodeTextarea1231").val(newCode); if(alreadyWriteCode1231!=code1231){ lastModifedTime1231 = new Date().getTime(); $("#rendering1231").show(); $("#rendered1231").hide(); } // alert(doc.getValue()); }); $(".CodeMirror").addClass("form-control"); // var editor1231 = CodeMirror.fromTextArea(, { // lineNumbers: true, // styleActiveLine: true, // matchBrackets: true, // theme:"eclipse", // }); editor1231.on("change",function(doc){ // alert(doc.getValue()); }); $("#tryDiv1231").hide(); }); $("div.codemirrorTips span").addClass("glyphicon glyphicon-asterisk");


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

checkName.jsp

最后附上checkName.jsp,现在看不明白不要紧,等学了JSP再来看,就会发现其实很简单。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <% String name = request.getParameter("name"); if("abc".equals(name)) out.print("<font color='red'>已经存在</font>"); else out.print("<font color='green'>可以使用</font>"); %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>

<%
	String name = request.getParameter("name");
	
	if("abc".equals(name))
	    out.print("<font color='red'>已经存在</font>");
	else 
		out.print("<font color='green'>可以使用</font>");
	
%>


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


关于 前端部分-Ajax-分步解析 的提问

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

上传截图