Ajax在静态页面中向指定url发送json请求获取返回的json数据
2016-07-19 10:57
1136 查看
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ document.getElementById("myDiv").innerHTML=xmlhttp.responseText; alert(xmlhttp.responseText); } } xmlhttp.open("GET","http://www.***.com/web/***/***.json",true); xmlhttp.send(); } </script> </head> <body> <h2>AJAX</h2> <button type="button" onclick="loadXMLDoc()">请求数据</button> <div id="myDiv"></div> </body> </html>
xmlhttp.open("GET","http://www.***.com/web/***/***.json",true);
第一个参数是请求方式,post、get,根据实际情况进行选择。第二个参数是请求的url。第三个参数设置请求是否为异步模式。如果是TRUE,JavaScript函数将继续执行,而不等待服务器响应。
xmlhttp.responseText是返回的数据。
利用document.getElementById("myDiv").innerHTML,将其放至该div块中进行显示。
相关文章推荐
- js 屏蔽 浏览器(IE和FireFox)的 刷新功能
- 全面了解JavaScript对象进阶
- JS操作JSON总结
- javascript的日期对象Date操作时间日期值
- javascript花式理解闭包
- Angular.js学习之一下载和搭建hello world实例
- Html,Css,Javascript及其他的注释方法详解
- JS中typeof与instanceof之间的区别总结
- url传递json对象
- js 格式化数字,html input type=datetime-local赋值
- JSON与MODEL互转
- js 动态添加元素(div、li、img等)及设置属性的方法
- Chrome调试js出现Uncaught SyntaxError: Unexpected identifier
- 教你轻松记住JS正则表达式
- JS把内容动态插入到DIV的实现方法
- js组件化1 input 框后span计算输入数字
- jsoup eg
- JS动态给对象添加事件的简单方法
- js操作字符串的常见方法
- js中的delete运算符