ajax 简单例子
2024-10-20 03:58:09
Html 代码:
<html>
<body> <div id="myDiv"><h3>Let AJAX change this text</h3></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button> </body>
</html>
js代码:
<script type="text/javascript"> var xmlhttp;
function loadXMLDoc() {
//ajax script goes here...
if(window.XMLHttpRequest) {
//code for IE7+, firefox, Chrome, Opera,Sarari
xmlhttp = new XMLHttpRequest();
}else {
//code for IE6,IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} if(xmlhttp != null) { //创建响应XMLHttpRequest对象状态变化的函数
xmlhttp.onreadystatechange = httpStateChange;
//创建http请求
xmlhttp.open('GET', 'json/data.json', true);
//发送http请求
xmlhttp.send(null); } } function httpStateChange() { console.log(xmlhttp.readyState, xmlhttp.status);
//请求中.
if(xmlhttp.readyState == 1) {
//loading..
document.getElementById('myDiv').innerHTML = 'loading..';
} ////异步调用完毕
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
console.log(data);
document.getElementById('myDiv').innerHTML = data.sites[1].Name;
}
} </script>
最新文章
- PHP设计模式(八)桥接模式(Bridge For PHP)
- curl模拟自动登陆&;采集网页数据
- Storm与Spark:谁才是我们的实时处理利器
- 那些臭名昭著的sql
- Linux常用性能检测命令解释
- C语言编写的随机产生四则运算测试题
- <;a>;多颜色标签点击之后保持原色的一次实践, Ext Panel下解决及通用方案思路
- 浏览器检测(BrowserDetect.js)
- Qt使用快捷键
- String,StringBuffer以及StringBuilder的差别
- go pkg
- 【LeetCode】87. Scramble String
- Python IDLE快捷键一览
- NOIP2012疫情控制(二分答案+倍增+贪心)
- LeetCode 63. Unique Path II(所有不同路径之二)
- Elasticsearch过滤器——filter
- g++基本用法
- 遇到ANR问题的处理步骤
- ArcGIS 常见错误
- Ubuntu postgres 内网 安装 卸载
热门文章
- 关于几类STL容器swap的复杂度问题
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
- Visual Studio 调试系列10 附加到正在运行的进程
- 动手学深度学习5-softmax回归
- PostMan测试REST接口时候,如何绕过登录的验证
- POJ 1306 暴力求组合数
- Redmine添加插件
- https的原理入门
- Mybatis系列(一)— 处理冲突字符
- [转] JS中arr.forEach()如何跳出循环