覆盖 XMLHttpRequest 并模拟响应

Override XMLHttpRequest and emulate response

是否可以覆盖 XMLHttpRequest 并模拟来自回调的响应?

类似于:

window.XMLHttpRequest.prototype.open = function() {
        var url = arguments[1];
        if (url === 'someUrl' && isOnline === false){
          //Return a fake reponse to the request...
        }
        ...
    }

在我的 phonegap 应用程序中执行此操作非常好,因此如果用户离线,我可以从我的本地 sqlite 数据库检索响应。

解决方案是使用 mockjax 插件。