Zjednodušení (spojení) více funkcí AJAXU – JavaScript, AJAX, jQuery – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Zjednodušení (spojení) více funkcí AJAXU – JavaScript, AJAX, jQuery – Fórum – Programujte.comZjednodušení (spojení) více funkcí AJAXU – JavaScript, AJAX, jQuery – Fórum – Programujte.com

 

Bart_
~ Anonymní uživatel
1 příspěvek
25. 3. 2020   #1
-
0
-

Ahoj,

začínám s javascriptem a s AJAXem. Mám server na arduinu a několik AJAX skriptů (bez jQuery) obsluhující webovou stránku (odesílání formulářů, aktualizace bloků, atd..). Většina skriptů se liší jen v počtu odesílaných proměnných, nebo v elementech, do kterých vpisuje informace.

Poradíte mi prosím, jak správně spojit tyto funkce, tak aby byl zápis co nejkratší? Určite nemusím v každé jednotlivé funkci kontrolovat odpověď serveru,


if (this.readyState == 4) { if (this.status == 200) { if (this.responseText != null)......
 

ale stačila by mi na to jedna funkce na kontrolu odpovědi serveru a poté volat podfunkce. Dále je podle mě zbytečné mít na každý formulář vlastní funkci, zase by stačila funkce na odesílání formulářů a podfunkce pro konkrétní formulář, atd..

Opravte mně pokud se pletu.

Díky za tipy.

<script>
function prehled() {
nocache = "&nocache="                                                         + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("prehledDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?prehled" + nocache, true);
request.send(null);
setTimeout('prehled()', 8000);
}
function dayCounterRefresh() {
nocache = "&nocache="                                                         + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("dayCounter").innerHTML = this.responseText;
}}}}
request.open("GET", "?refreshDC" + nocache, true);
request.send(null);
setTimeout('dayCounterRefresh()', 15000);
}
function datumKalibraceRefresh() {
nocache = "&nocache="                                                         + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("datumKalibraceDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?datumKalibraceRefresh" + nocache, true);
request.send(null);
setTimeout('datumKalibraceRefresh()', 60000);
}
</script>
<script type="text/javascript">
function loadProfil(DTnas, DHnas, DCnas, trvaniDneHod, SzapHod, SzapMin, NTnas, NHnas, NCnas, Thystereze, Hhystereze, Chystereze){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("profilDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?td=" + DTnas + "&hd=" + DHnas + "&cd=" + DCnas + "&dh=" + trvaniDneHod + "&zh=" + SzapHod + "&zm=" + SzapMin + "&tn=" + NTnas + "&hn=" + NHnas + "&cn=" + NCnas + "&th=" + Thystereze + "&hh=" + Hhystereze + "&ch=" + Chystereze + nocache, true);
xmlhttp.send();
}
function loadZalivka(xD, yD, xN, yN){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("zalivkaDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?xd=" + xD + "&yd=" + yD + "&xn=" + xN + "&yn=" + yN + nocache, true);
xmlhttp.send();
}
function loadKalibrace(kalibrace){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("kalibraceDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?co=" + kalibrace + nocache, true);
xmlhttp.send();
}
function loadInflux(influx, dbName, table){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("influxDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?ii=" + influx + "&db=" + dbName + "&tb=" + table + nocache, true);
xmlhttp.send();
}
function dayCounter() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("dayCounter").innerHTML = this.responseText;
}}}}
request.open("GET", "?dayCounter" + nocache, true);
request.send(null);
}
function loadRelay(radio, zapProgramHod, programHodStart, programMinStart, xP, yP){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("relayDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?pz=" + radio + "&tp=" + zapProgramHod + "&ph=" + programHodStart + "&pm=" + programMinStart + "&xp=" + xP + "&yp=" + yP + nocache, true);
xmlhttp.send();
}
function loadTimeZone(timeZone){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("timeZoneDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?tz=" + timeZone + nocache, true);
xmlhttp.send();
}
function loadUrlGraf(grafyURL){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("urlGrafDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?ga=" + grafyURL + nocache, true);
xmlhttp.send();
}
function startNepProvoz() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("zalivkaDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?startNepProvoz" + nocache, true);
request.send(null);
}
function startWatering() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("zalivkaDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?startWatering" + nocache, true);
request.send(null);
}
function sendInflux() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("influxDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?sendInflux" + nocache, true);
request.send(null);
}
function sendAirControl() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("switcherDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?sendAir" + nocache, true);
request.send(null);
}
function sendLightControl() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("switcherDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?sendLight" + nocache, true);
request.send(null);
}
function checkTime() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("timeSpan").innerHTML = this.responseText;
}}}}
request.open("GET", "?checkTime" + nocache, true);
request.send(null);
}
function loadConstantIP(konstantIP){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("konstantIPDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?si=" + konstantIP + nocache, true);
xmlhttp.send();
}
function enableConstantIP() {
nocache = "&nocache=" + Math.random() * 10;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText != null) {
document.getElementById("konstantIPDiv").innerHTML = this.responseText;
}}}}
request.open("GET", "?constantIP" + nocache, true);
request.send(null);
}
function loadWebPassword(pass, pass2, passVal){
nocache = "&nocache=" + Math.random() * 10;
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("webPasswordDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","?pass=" + pass + "&pas2=" + pass2 + "&pasv=" + passVal + nocache, true);
xmlhttp.send();
}
</script>
Nahlásit jako SPAM
IP: 37.188.191.–
gna
~ Anonymní uživatel
1849 příspěvků
25. 3. 2020   #2
-
0
-

Nevím jistě na co se ptáš.

U každého requestu potřebuješ zkontrolovat výsledek, ale můžeš to mít v jedné funkci.

A pokud chceš sloučit víc různých requestů, tak prostě zpracovávej a vracej víc dat.

<?php
$response = [];
if (isset($_GET['prvni'])) {
    $response['prvni'] = 'prvniiii';
}
if (isset($_GET['druhy'])) {
    $response['druhy'] = 'druhyyyy';
}
if (isset($_GET['treti'])) {
    $response['treti'] = 'tretiiii';
}
echo json_encode($response);
<script>
function refreshData(qs) {
    var request = new XMLHttpRequest();
    request.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200 && this.responseText != null) {
        var resp = JSON.parse(this.responseText);
        console.log(resp);
        if (resp.prvni !== undefined)
            document.getElementById("prvni").innerHTML = resp.prvni;
        if (resp.druhy !== undefined)
            document.getElementById("druhy").innerHTML = resp.druhy;
        if (resp.treti !== undefined)
            document.getElementById("treti").innerHTML = resp.treti;
        }
    }
    var nocache = "&nocache=" + Math.random() * 1000000;
    request.open("GET", "?" + qs + nocache, true);
    request.send(null);
}
function startRefresh() {
    setInterval(function() { refreshData("prvni&druhy"); }, 8000);
    setInterval(function() { refreshData("treti"); }, 15000);
}
</script>
Nahlásit jako SPAM
IP: 213.211.51.–
gna
~ Anonymní uživatel
1849 příspěvků
25. 3. 2020   #3
-
0
-

Vypadá to nějak divně, asi mi někde utekly nějaké závorky :) ale pointa by měla být jasná.

Nahlásit jako SPAM
IP: 213.211.51.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 12 hostů

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý