I'm trying to override XMLHTTPRequest behaviour in order to delegate Ajax requests to a phonegap plugin in case of a native app (Which will handle https certificate authentication challenge).

In case the application is running in native container,
  • Cancel current HTTP call
  • Call the phonegap plugin in order to execute HTTP call
  • Receive HTTP response
  • Trigger an event in current XMLHttpRequest instance with http response elements

My apps must run on a native et web context using the same source code. Developers can do Ajax calls seamlessly.

Here is a sample code for what I'm trying to do:

XMLHttpRequest.prototype.send = function() {
    var me = this;
    cordova.exec(function(httpresult) {             
        // Query success
        // httpresult object contains http response headers and content
        // How can onreadystatechange be triggered ???
    }, function(httpresult) {
        // Query failure
    }, "QueryPlugin", "query", [{
        url: me.url,
        method: me.method,
        data: me.data
        // other http query params
I don't know how to pass http headers and content properly to the current XMLHttpRequest instance and how to trigger onreadystatechange method.

If you have any ideas. Thanks in advance,