搜索文档
首页
HTML/CSS
JavaScript
服务端开发
Java教程
移动端
数据库
当前位置:
首页
JavaScript
AJAX 教程
AJAX 基础教程
源代码
清空
点击运行
<!DOCTYPE html> <html> <title> Constants and Literals示例 - 基础教程(div.cn)</title> <body> <h1>XMLHttpRequest 对象</h1> <p>单击按钮发送请求并从服务器检索数据:</p> <button type=" button" onclick="fetchDoc()">发出请求</button> <p id="result"></p> <script> function fetchDoc() { var httpRequest; if(window.XMLHttpRequest) { // 主流浏览器 (Chrome, Mozilla, Safari, IE7+, ...) httpRequest = new XMLHttpRequest(); }else if (window.ActiveXObject) { //IE6或IE早期版本 httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } httpRequest.onreadystatechange = function() { if(this.readyState == 4 && this.status == 200) { document.getElementById("result").innerHTML = this.responseText; } }; httpRequest.open("GET", "ajax_data.txt", true); httpRequest.send(); } </script> </body> </html>
运行结果