function AjaxRequest(params, url) {
if (params) {
this.params = params;
this.type = "GET";
this.url = url;
// this.contentType = "multipart/form-data";
this.contentLength = params.length;;
}
}
AjaxRequest.prototype.createXmlHttpObject = function() {
try {
this.xmlHttp = new XMLHttpRequest();
}
catch (e) {
try {
this.xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
catch (e) {}
}
if (!this.xmlHttp) {
alert("Error creating XMLHttpRequestObject");
}
}
AjaxRequest.prototype.process = function() {
try {
if (this.xmlHttp) {
document.getElementById("loading"+this.params).innerHTML = "processing...";
document.getElementById("loading"+this.params).className = "progressing";
this.xmlHttp.onreadystatechange = this.handleRequestStateChange();
this.xmlHttp.open(this.type, this.url, true);
this.xmlHttp.setRequestHeader("Content-Type", this.contentType);
this.xmlHttp.setRequestHeader("Content-Length", this.contentLength);
this.xmlHttp.send(this.params);
}
}
catch (e) {
document.getElementById("loading"+this.params).innerHTML = "";
alert("Failed to establish connection with server");
}
}
AjaxRequest.prototype.handleRequestStateChange = function() {
var self = this;
return function() {
try {
if (self.xmlHttp.readyState == 4 && self.xmlHttp.status == 200) {
self.handleServerResponse(); // calling the method handleServerResponse
}
}
catch (e) {
alert(self.xmlHttp.statusText);
}
};
}
AjaxRequest.prototype.handleServerResponse = function() {
try {
if(this.xmlHttp.responseText.length > 0){
document.getElementById("loading"+this.params).innerHTML = this.xmlHttp.responseText;
}
}
catch (e) {
alert("Error reading server response");
}
}
function CreateAjaxControl(params, url){
var con = $("#"+params+" select").val();
url += "?id="+params+"&con="+con;
var ajaxRequest = new AjaxRequest(params, url);
ajaxRequest.createXmlHttpObject();
ajaxRequest.process();
ajaxRequest.count = 0;
ajaxRequest.progress = CheckingProgress;
ajaxRequest.progress(ajaxRequest, ajaxRequest.params, ajaxRequest.count);
//var ajaxRequest = new AjaxRequest(params, url);
//ajaxRequest.checking = setInterval(function(){CheckingProgress(ajaxRequest.params);}, 100);
}
//function Check(id){
// var res = 0;
// while( res != "done..."){
// res = CheckingProgress(id, res);
// }
}
function CheckingProgress(obj, id, count){
var self = obj;
if (window.XMLHttpRequest){
xmlhttp8 = new XMLHttpRequest();
}else{
xmlhttp8 = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp8.onreadystatechange = function(){
if (xmlhttp8.readyState==4 && xmlhttp8.status==200){
var result = xmlhttp8.responseText.split(',');
document.getElementById("loading"+id).innerHTML = result[0] + " out of " + result[1];
self.count = result[0];
self.progress(self, self.params, self.count);
}else if(xmlhttp8.status==404){
document.getElementById("loading"+id).innerHTML = "done...";
//return "done";
}
}
xmlhttp8.open("GET","views/test2.php?id="+id+"&count="+count,true);
xmlhttp8.send();
}
I have implemented a long polling script that continuously fetches the number of updated records from my database. Though it functions correctly when there are no updates, it encounters an issue when I update a record in the database - a new ajax request is created with the updated count as a parameter while the original request remains unchanged. What could be causing this problem? Is it related to how I assigned my long polling method to an object? Any insights would be greatly appreciated.