使用ajax从服务器端获取数据

来源:互联网 发布:交大附中嘉定分校知乎 编辑:程序博客网 时间:2024/06/07 02:03
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><script>/* * ajax分为四步使用 *//* * 第一步:得到XMLHttpRequest  * 如果存在IE6的用户,要做兼容 */var xhr = new XMLHttpRequest();/* * open主要有三个参数 * 第一个参数是一个字符串,代表html的请求方式:GET、POST,注意:大写 * 第二个参数表示我们要访问的地址 uri * 第三个参数是一个boolean值,表示是否异步 *  */xhr.open("POST","ajax.do",true);/* * http请求的状态监听 */xhr.onreadystatechange = function() {if (xhr.readyState == 4 && xhr.status == 200) {/* * ajax请求,服务器端返回的数据格式只有两种 * 字符串 * xml */var msg = document.getElementById("msg");msg.innerHTML = xhr.responseText;}};/* * 发送ajax请求 * xhr.send(null); *  * post请求时,我们将参数传递在send方法中 *  */xhr.send();//xhr.send(null);//xhr.send(username=liujianhong&pwd=123);</script></head><body><h1 id="msg"></h1></body></html>



0 0
原创粉丝点击