【问题标题】:How to Modify XHR如何修改 XHR
【发布时间】:2017-07-19 11:09:42
【问题描述】:

我有一个 XHR 对象。当 send() 方法被触发时,我想在没有服务器、任何网络操作的情况下提供响应。

 var origSend = xhr.send;
xhr.send = function () {      
        waitForFile(xhr, url).then(responseData=>{
           //ı wantto that responseData , send to xhr response
           //this responseData comes from anywhere(webrtc,fetch,filesystem,localstorage,......) 
         }); // side-effect
     FileRequests.next({url: url})
    //origSend.call(xhr, arguments)  //I don't want to call original send method
}

//等待响应的代码

xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
    console.log(xhr.responseText);
}
}

【问题讨论】:

    标签: javascript xmlhttprequest


    【解决方案1】:

    我解决了。

          Object.defineProperty(xhr, 'readyState', {
                writable: true,
                configurable: true
            });
            Object.defineProperty(xhr, 'status', {
                writable: true,
                configurable: true
            });
            Object.defineProperty(xhr, 'statusText', {
                writable: true,
                configurable: true
            });
            Object.defineProperty(xhr, 'responseText', {
                writable: true,
                configurable: true
            });
            Object.defineProperty(xhr, 'responseType', {
                writable: true,
                configurable: true
            });
            Object.defineProperty(xhr, 'response', {
                writable: true,
                configurable: true
            });
            xhr.readyState = 4;
            xhr.status = 200;
            xhr.statusText = "OK";
            xhr.responseType = "blob";
            xhr.response = data.blob; // whatever you want 
            xhr.onreadystatechange();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 2016-08-05
      • 2012-01-24
      相关资源
      最近更新 更多