|
步骤
1
:
AJAX 请求和相应图示
步骤
2
:
创建XHR
步骤
3
:
设置响应函数
步骤
4
:
设置并发出请求
步骤
5
:
处理响应信息
步骤
6
:
再完整的演示一遍
步骤
7
:
checkName.jsp
创建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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
XHR对象的作用是和服务器进行交互,所以既会发消息给服务器,也能接受服务器返回的响应。
当服务器响应回来的时候,调用怎么处理呢?
通过 xmlhttp.onreadystatechange=checkResult 就可以指定用checkResult 函数进行处理。
通过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)
在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的内容为服务端传递回来的文本
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>
<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 全屏编辑,再次点击恢复
|
渲染中
渲染完成
|
最后附上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>");
%>
代码高亮插件双击即可选中,不过部分同学反应,通过代码高亮插件复制的代码无法在IDEA里正常显示,这里提供TEXTAREA的方式,方便复制,谢谢
1. 自行完成练习
根据练习目标尽量自己实现代码效果,期间会碰到疑问,难题,和自己不懂的地方,这些都是必要的过程
2. 带着疑问查看答案
完成过程中,碰到无法解决的问题,带着疑问,查看答案,分析答案的解决思路
3. 查看答案讲解视频
依然有不明白的地方,点开视频讲解,带着疑问,听视频讲解有问题的部分
4. 再做一遍
理解后,再从头做一遍,把有疑问的地方都捋清楚
5. 总结
最后再总结一遍,总结思路,总结解决办法,以后遇到类似的问题,怎么处理
HOW2J公众号,关注后实时获知布最新的教程和优惠活动,谢谢。
提问之前请登陆
|