Search code examples
javascriptcordovaxmlhttprequestpython-requestsphonegap

Override XMLHttpRequest and emulate response


Would it be possible to override XMLHttpRequest and emulate response from a callback ?

Something like :

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

It would be realy nice to do this in my phonegap app, so if the user is offline I could retrieve the response from my local sqlite db.


Solution

  • The solution was to use mockjax plugin.