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;
me.abort();
cordova.exec(function(httpresult) {
// Query success
// httpresult object contains http response headers and content
// How can onreadystatechange be triggered ???
me.onreadystatechange(httpresult);
}, 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.
Thanks in advance,
Cordova/phonegap redefines XMLHttpRequest object out of the box for some of the platforms providing additional functionality, for example for Windows Phone it does it in the following way
https://github.com/apache/incubator-cordova-js/blob/master/lib/wp7/plugin/wp7/XHRPatch.js
Probably you would like to do something in the same manner or extend existing implementation for your platform.