How to load value from a variable through Ajax

Asked

Viewed 189 times

1

I did the following in an html file:

    <script>
    //transmite as informações
    var xmlHttp = GetXmlHttpObject();
    function show() {
        if (xmlHttp == null) {
            alert ("Seu browser não suporta AJAX!");
            return;
        }else{
            var url = "pagina.html";  //pagina que contem apenas uma frase simples sem formatação
            xmlHttp.onreadystatechange = stateChanged;
            xmlHttp.open("POST",url,true);
            xmlHttp.send(null);
        }
    }        

    //verifica o status do carregamento da pagina
    function stateChanged() {
        if (xmlHttp.readyState==4) {
            document.getElementById("texto").innerHTML=xmlHttp.responseText;
        }
    }

    //pega o objeto xmlHTTP do navegador
    function GetXmlHttpObject() {
        var xmlHttp = null;
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            // Internet Explorer
            try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
       }
        return xmlHttp;
    }

    window.setInterval(show, 10000);
    </script>
    </head>
    <body>
    <div id="texto"></div>
    <script>show();</script>
    </body>
    </html>

So I would like to capture a value of a local variable within the same script instead of going in another file to fetch such information.

Anyone who can help, thank you

  • It is not very clear to me what you are trying to do. Can you give some example?

  • I’m developing an IOT project with a wifi module... It will have its home page (only file, that is, only the index), where in it will show information of temperature and humidity, as well as the time of an rtc mobile. These data are contained in variables, so I need to access these variables without the need to reload the page, but the only examples with ajax I saw are accessing data in other files (html, php...) understand?

  • 1

    Dude, You can use this old Ajax code. It’s from the time of the stones (there is no more browser that has not supported ajax for years), search and use the fetch that works on modern browsers. You want this data to come from your Arduino/ESP/etc. Then you will have to create a dynamic page, like "/data.json" and program your IOT device to write on this page the values you want. Then in another page "/index.html" you will have a JS that from time to time makes the ajax request pro your '/data.json' and shows on the screen.

  • Hmm, thanks I’ll try

  • 1

    Adding to @Michelleakemi’s comment, instead of staying in a loop by checking the status you can search for socket that checks in real time.

  • Opa Lucas Costa, yes I had seen on sockets, I have not implemented yet, but it was worth the tip ;)

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.