美文网首页
XMLHttpRequest

XMLHttpRequest

作者: 假装会编程 | 来源:发表于2016-10-30 12:50 被阅读0次

    XMLHttpRequest

    • 创建:
    var createXHR = function(){
        var xhr = false;
        try{
            xhr = new XMLHttpRequest();//尝试直接创建,适用于IE以外的大多数浏览器
        }
        catch(e){
            try{
                xhr = new ActiveXObject('Mxsm112.XMLHTTP');//尝试使用较新版本IE的创建方式
            }
            catch(e){
                try{
                    xhr = new ActiveXObject('Microsoft.XMLHTTP');//尝试使用较老版本IE的创建方式
                }
                catch(e){
                    xhr = false;//创建失败,返回false提示
                    console.log(e.name + ' : ' + e.message);
                }
            }
        }
        return xhr;
    }
    
    • 方法:
    • open(method,url,async) :
    • method指定请求使用的方法,包括GET、POST、HEAD、DELETE;
    • url即请求地址;
    • async规定是否启用异步请求,异步请求将直接返回,需要使用事件监听器在响应就绪后动态处理,默认为true;
    • send() : 发送请求并根据同步异步设定适时返回,接受一个可选的请求体作为参数(在GET和HEAD中被忽略);
    • abort() : 立即中断已经发送的请求;对应请求的就绪状态被置为0,但不会触发readystatechange事件;
    • onreadystatechange() : 当readyState发生变化时执行的事件处理函数;
    • ontimeout() : 请求超时后调用的方法;
    • 属性:
    • readyState:
    • 0 请求未初始化,open()未调用;
    • 1 请求已建立,未发送,send()尚未调用;
    • 2 请求已发送,处理中,可以从响应中获取首部(header)和状态(status);
    • 3 响应处理中,响应中已有部分数据可用,但未完全就绪;
    • 4 响应已就绪;
    • 注意,各浏览器中对就绪状态的处理并不一致,不能过于依赖状态来执行操作,测试在chrome(53)和Firefox(43)中onreadystatechange方法只能在状态1和4时被调用,IE(11)则更奇特,依次出现了1、1、2、3、4的状态,1出现了两次。
    • 无法通过onreadystatechange()仅在就绪状态发生改变时被调用,但在readyState被在请求中断(abort()被调用)被置0时不会触发该方法,因而无法通过该方法获取readyState为0的状态,只能在请求未初始化或被中断时手动获取;
    • upload:
    • 返回一个代表上传过程的对象,可以在其上注册事件监听器以追踪上传进程;
    • 可选事件监听器包括
    • onloadstart : the fetch starts (请求开始装载数据时调用的方法)
    • onprogress : data transfer is going on (周期性调用的反馈上传进度的方法)
    • onabort : the fetch operation was aborted (请求中断后调用的方法)
    • onerror : the fetch failed (请求中出现错误时调用的方法)
    • onload : the fetch succeeded (请求在抓取内容成功后返回时调用的方法)
    • ontimeout : the fetch operation didn't complete by the timeout the author specified (请求超时调用的方法)
    • onloadend : the fetch operation completed (either failed or succeeded)
    • response:
    • 返回相应的实体主体(response entity body),具体类型取决于responseType的值,在1.0中只有DOMString和 Document,在2.0中扩充了Blob、FormData、ArrayBuffer、File等;
    • 当responseType为‘text’或空,response在请求处于loading状态时保存部分响应文本(response text);
    • responseType: 返回一个定义响应实体类型的枚举值
    • responseText: 返回一个包含响应文本(response as text)的DOMString,或者null;
    • responseXML: 返回一个包含响应的Document,或者null(请求未发送,响应无法解析);
    • status: 返回响应的状态码;
    • statusText: 返回响应的状态码及服务器的反馈信息;
    • timeout: 定义请求在被终结前可以维持的最大时长,以毫秒为单位;
    • withCredentials: 一个布尔值,指示了Cross Site Access Control请求是否需要使用证书(如cookies,授权首部(authorization headers)或者TLS用户证书等);该值的设定对同域请求没有影响;
    • In addition, this flag is also used to indicate when cookies are to be ignored in the response. The default is false. XMLHttpRequest from a different domain cannot set cookie values for their own domain unless withCredentials is set to true before making the request. The third-party cookies obtained by setting withCredentials to true will still honor same-origin policy and hence can not be accessed by the requesting script through document.cookie or from response headers.

    相关文章

      网友评论

          本文标题:XMLHttpRequest

          本文链接:https://www.haomeiwen.com/subject/xtquuttx.html