首页 > Web开发 > 详细

ajax基础

时间:2017-07-25 22:23:31      阅读:306      评论:0      收藏:0      [点我收藏+]
  function ajax(url, fn) {
       //创建对象
       // IE早期版本是new ActiveXObject() 高级版本已不支持
        var xhr = new XMLHttpRequest();
        
        
        //打开链接 
        xhr.open(‘GET‘, url, true);
        //发送请求
        xhr.send(null);

        xhr.onreadystatechange = function () {// 状态发生变化时,函数被回调
            // 监听readystate
           if (xhr.readyState === 4) {
                
                if (xhr.status === 200) {
                    // 成功,通过responseText拿到响应的文本:
                     console.log(xhr.responseText);
                } else {
                    // 失败,根据响应码判断失败原因:
                    console.log(xhr.status);
                }
           }else {
                // HTTP请求还在继续...
            }
        }
    }                                

ajax基于XMLHttpRequest

关键方法:

XMLHttpRequest
open
send
readystatechange

关键属性指令:

readyState
status
responseText(还有responseXML )

ajax基础

原文:http://www.cnblogs.com/zyjzz/p/7236458.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!