AJAX教程系列四:异步数据获取与处理

来源:互联网 发布:淘宝商品为什么会下架 编辑:程序博客网 时间:2024/05/07 01:21

一:在上篇教程中忘记给出数据获取代码示例。代码如下

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script>    window.onload = function() {        var oBtn = document.getElementById('btn');          oBtn.onclick = function() {            var xhr = null;            // 打开浏览器            try {                xhr = new XMLHttpRequest();            } catch (e) {                xhr = new ActiveXObject('Microsoft.XMLHTTP');            }            xhr.open('post','list.php',true);            xhr.send('');            // 等待服务器返回内容            xhr.onreadystatechange = function() {                if(xhr.readyState == 4){                    if(xhr.status == 200){                        alert(xhr.responseText);                    }else {                        alert("错误"+xhr.status);                     }                }               }         }    }</script></head><body>    <input type="button" value="按钮" id="btn" /></body></html>
0 0
原创粉丝点击