function sendRequest(settings) {
settings = {
type: settings.type || "POST",
url: settings.url || "",
timeout: settings.timeout || 5000,
onComplete: settings.onComplete || function(){},
onError: settings.onError || function(){},
onSuccess: settings.onSuccess || function(){},
data: settings.data || ""
};
if (typeof XMLHttpRequest == "undefined") {
XMLHttpRequest = function(){
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
return null;
};
}
var xhr = new XMLHttpRequest();
xhr.open(settings.type, settings.url, true);
var timeoutVal = settings.timeout;
var requestFinished = false;
setTimeout(function(){
requestFinished = true;
}, timeoutVal);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && !requestFinished) {
if (httpSuccess(xhr)) {
settings.onSuccess(httpData(xhr, settings.type));
} else {
settings.onError();
}
settings.onComplete();
xhr = null;
}
};
xhr.send();
function httpSuccess(response) {
try {
return (response.status >= 200 && response.status < 300) || response.status == 304 || navigator.userAgent.indexOf("Safari") >= 0 && typeof response.status == "undefined";
} catch(e) {
return false;
}
}
function httpData(response, responseType) {
var contentType = response.getResponseHeader("content-type");
var data = !responseType && contentType && contentType.indexOf("xml") >= 0;
data = responseType == "xml" || data ? response.responseXML : response.responseText;
if (responseType == "script") eval.call(window, data);
return data;
}
}
function encode(data) {
var serializedData = [];
if (data.constructor == Array) {
for (var i = 0; i < data.length; i++) serializedData.push(data[i].name + "=" + encodeURIComponent(data[i].value));
} else {
for (var key in data) serializedData.push(key + "=" + encodeURIComponent(data[key]));
}
return serializedData.join("&");
}
The issue with this code needs to be identified. What do you think is causing the problem?