I am new in javascript and Ajax, would need to retry 3 times if ajax response is not 200.
Ajax Function -
function fireAndForget(strURL) {
log("will try to invoke... [ " + strURL + " ]");
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
} // IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
if(self.xmlHttpReq.status == 200) {
log("received JSON response : [" + self.xmlHttpReq.responseText + "]");
var resObj = parseJSON(self.xmlHttpReq.responseText);
if("handled" in resObj) {
if(resObj.handled) {
if("success" in resObj) {
if(resObj.success) {
// DO NOTHING
} else {
if(resObj.message) {
alert(resObj.message);
}
}
}
}
} else {
log("auth update notification was not handled. response : [" + self.xmlHttpReq.responseText + "]");
}
} else {
// unable to contact the auth update listener
alert("<%=pNotifyFailedMsg%>");
log("unable to contact listener URL @ [" + strURL + "]");
}
}
};
// fire a get request with the SSO information
self.xmlHttpReq.send(null);
//alert("sent url : [" + strURL +"]");
}
Need to add retry on
if(self.xmlHttpReq.status == 200) {
log("received JSON response : [" + self.xmlHttpReq.responseText + "]");
var resObj = parseJSON(self.xmlHttpReq.responseText);
if("handled" in resObj) {
if(resObj.handled) {
if("success" in resObj) {
if(resObj.success) {
// DO NOTHING
} else {
if(resObj.message) {
alert(resObj.message);
}
}
}
}
} else {
log("auth update notification was not handled. response : [" + self.xmlHttpReq.responseText + "]");
}
} else {
// unable to contact the auth update listener
alert("<%=pNotifyFailedMsg%>");
log("unable to contact listener URL @ [" + strURL + "]");
}
I have tried with loop and some other solution in else part of above code, but didn't work, Kindly help. What should be the good approach for retry in such cases
Have to show alert(else part) only after 3 retry