xmlHttpRequest.responseText issue (undefined)

271 Views Asked by At

I'm trying to generate dynamic variables for my form web page that runs on my Siemens S7-1200 PLC. The issue that I'm having is that as most issues (as far as I've read) is that my xmlhttprequest.responseText is working if I do console.log(xhttp.responseText) but I can't get the value of the responseText into a variable as those stay "undefined". Even when I try putting it into a global variable. I've seen quite a lot of answers with "callback" but I have no idea what this means.

Here's my code:

var json

function refreshVar(){


    if(window.XMLHttpRequest){
        xhttp = new XMLHttpRequest();
    } else {
        xhttp = new ActiveXobject("Microsoft.XMLHTTP");
    }

        xhttp.onreadystatechange = function()
        {
            if(xhttp.readyState == 4 && xhttp.status == 200)
            {   
        console.log(xhttp.responseText);
        console.log(json);
        json = xhttp.responseText;

            }
        }
        xhttp.open("GET", "IOCounter.html",false); 
    xhttp.send();

}

and here's an image of the result in the console:

https://i.stack.imgur.com/zUHNM.png

in the "xhttp.open()" function I've both tried false and true and there was no difference. I've also tried getting a "return(xhttp.responseText)" which didn't work either

The function is also ran in a repeating loop every 30ms so it updates more than enough. Keep in mind this isn't the whole code and some stuff is censored due to secrecy of my company.

I hope I'll get some help soon!

Thanks already!

0

There are 0 best solutions below