I have been writing a script that can redirect AJAX request to other server when internet is not available, keep the request to same route when internet is available My script is doing the same it replaces the script with local server ip whenever data connection is not available, but its not effecting the functionality of the AJAX,
var REMOTE_SERVER="http://opensupermall";
var LOCAL_SERVER="http://127.0.0.1:909";
var custom_url="";
var open = window.XMLHttpRequest.prototype.open,
send = window.XMLHttpRequest.prototype.send;
function openReplacement(method, url, async, user, password) {
custom_url = url;
if(navigator.onLine){
custom_url = url.replace(REMOTE_SERVER,LOCAL_SERVER);
}
console.log("openReplacement:"+custom_url);
this._url = custom_url;
return open.apply(this, arguments);
}
function sendReplacement(data) {
if(this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
console.log('Request sent to:'+this._url);
this.onreadystatechange = onReadyStateChangeReplacement;
return send.apply(this, arguments);
}
function onReadyStateChangeReplacement() {
console.log('Url Changed to: ', this._url);
if(this._onreadystatechange) {
return this._onreadystatechange.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
I need suggestion that is this possible right i am doing? Over riding of ajax request is possible ?
I want to change the URL on every ajax request made !
Can you help me ? Whats wrong in this code if its possible to override ajax request before it hits the server ?
You can use this code block,(jquery ajax) (you can create a utility javascript file and placed these code in this util script. And then, use every where)
Usage =>