var apiKey = '5cf9e55f1a7a12c9a8e06ffc2a039cd5';
var ID = 392;
var domain = "api.adatjogasz.hu/v1";
var loading = "Betöltés...";
var corsNotSupported = "CORS nem támogatott.";
var errorMSG = "Hiba a lekérdezés során.";
makeCorsRequest(apiKey,ID);
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function makeCorsRequest(apiKey, ID) {
var target = document.documentElement;
while (target.childNodes.length && target.lastChild.nodeType == 1) {
target = target.lastChild;
}
var elem = target.parentNode;
elem.innerHTML = loading;
var url = "//" + domain + "/getContract.php?apiKey=" + apiKey + "&reqID=" + ID;
var xhr = createCORSRequest('GET', url);
if (!xhr) {
elem.innerHTML = corsNotSupported;
return;
}
xhr.onload = function() {
var text = xhr.responseText;
elem.innerHTML = text;
};
xhr.onerror = function() {
console.log(xhr);
elem.innerHTML = errorMSG;
};
xhr.send();
var file = document.createElement("link");
file.setAttribute("rel", "stylesheet");
file.setAttribute("type", "text/css");
file.setAttribute("href", "//" + domain + "/getStyle.php?apiKey=" + apiKey + "&reqID=" + ID);
document.head.appendChild(file);
}
function makeCorsRequestfromID(apiKey, ID, elemID) {
var elem = document.getElementById(elemID);
elem.innerHTML = loading;
var url = "//" + domain + "/getContract.php?apiKey=" + apiKey + "&reqID=" + ID;
var xhr = createCORSRequest('GET', url);
if (!xhr) {
elem.innerHTML = corsNotSupported;
return;
}
xhr.onload = function() {
var text = xhr.responseText;
elem.innerHTML = text;
};
xhr.onerror = function() {
elem.innerHTML = errorMSG;
};
xhr.send();
}
function makeCorsRequestfromClassName(apiKey, ID, elemName) {
var elem = document.getElementsByClassName(elemName);
for (i = 0; i < elem.length; i++) {
elem[i].innerHTML = loading;
}
var url = "//" + domain + "/getContract.php?apiKey=" + apiKey + "&reqID=" + ID;
var xhr = createCORSRequest('GET', url);
if (!xhr) {
for (i = 0; i < elem.length; i++) {
elem[i].innerHTML = corsNotSupported;
}
return;
}
xhr.onload = function() {
var text = xhr.responseText;
for (i = 0; i < elem.length; i++) {
elem[i].innerHTML = text;
}
};
xhr.onerror = function() {
for (i = 0; i < elem.length; i++) {
elem[i].innerHTML = errorMSG;
}
};
xhr.send();
}