AJAX
2016-07-01 10:04
337 查看
摘要: 最近学习了JS,用JS实现了Ajax
1. 在eclipse创建一个动态web项目
2. 创建一个ajaxPost.html,内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//创建XMLHttpRequest对象
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) { //浏览器兼容性
xmlhttp = new XMLHttpRequest();
} else { //兼容iE6一下的版本
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//结果返回后,页面处理
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
//发送请求
xmlhttp.open("post", "demoPost.jsp", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
xmlhttp.send("city=北京&name=xiaohua");
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>
3. 再创建一个接受请求的demoPost.jsp,内容如下:
<%
//设置编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String fname=request.getParameter("name");
String fcity=request.getParameter("city");
out.print("Dear " + fname + ".");
out.print("Hope you live well in " + fcity + ".");
%>
4. 启动Tomcat,在浏览器中输入:http://localhost:8080/AJAX/ajaxPost.html,回车可以看到下面信息:
5. 点击“请求数据”按钮,我们看到有数据返回到页面上来了,详见下图:
6. AJAX的具体实现见文章粗体部分。
1. 在eclipse创建一个动态web项目
2. 创建一个ajaxPost.html,内容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//创建XMLHttpRequest对象
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) { //浏览器兼容性
xmlhttp = new XMLHttpRequest();
} else { //兼容iE6一下的版本
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//结果返回后,页面处理
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
//发送请求
xmlhttp.open("post", "demoPost.jsp", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
xmlhttp.send("city=北京&name=xiaohua");
}
</script>
</head>
<body>
<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="myDiv"></div>
</body>
</html>
3. 再创建一个接受请求的demoPost.jsp,内容如下:
<%
//设置编码方式
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String fname=request.getParameter("name");
String fcity=request.getParameter("city");
out.print("Dear " + fname + ".");
out.print("Hope you live well in " + fcity + ".");
%>
4. 启动Tomcat,在浏览器中输入:http://localhost:8080/AJAX/ajaxPost.html,回车可以看到下面信息:
5. 点击“请求数据”按钮,我们看到有数据返回到页面上来了,详见下图:
6. AJAX的具体实现见文章粗体部分。
相关文章推荐
- Android---ScrollView滚动
- thinkphp初学者的安装与配置
- 应用MVP模式写出可维护的优美Android应用
- Android 中的MVP 模式
- Go to the first line OR the last line of the file
- 关于SASS
- VS2010 调试窗口一闪而过解决方法
- [置顶] Android开发之华为手机无法看log日志解决方法(亲测可用华为荣耀6)
- nginx介绍
- C语言编写的简单计算器程序
- Machine Schedule(poj 1274)
- [转]12篇学通C#网络编程——第二篇 HTTP应用编程(上)
- idea 15注册方法 破解方法 注册码
- dtrace 手册
- linux中断申请之request_threaded_irq
- ASP.NET MVC Json()处理大数据异常解决方法,字符串的长度超过了为 maxJsonLength
- js判断是否微信打开
- adb shell am 的用法
- 重建二叉树-剑指offer
- linux j2ee环境搭建