Fetch informations from a Homepage

Dear all,

I did not find if and how it is possible to communicate with an URL.

I want to open an link https: / / test . de?x y=1’ and receive some string to work with.

Does someone has an information how to do iT?

Many thanks.

1 Like

You can use XMLHttpRequest().

This is how I do it. There are probably more elegant ways but this works for me. This also has a timeout function I implemented which is optional. Just set to the amount of time you want to give if before running some code in the case it times out without a response.

var connectedOK=false;//optional  
let httpReq = new XMLHttpRequest();
let url="https://yourURL...";
httpReq.open("GET", url);
httpReq.send();

httpReq.onreadystatechange = async function () {
 
var serverMessage=await httpReq.responseText;
connectedOK=true;//optional
      
//Do something with server message here    


}

//optional
setTimeout(function(){ 
if(connectedOK==false){

//Do something for connection timeout

} 
}, 3500);
1 Like

fetch should work as well, but does’t support setting a timeout:

try {
    const response = await fetch(url);
    if (response.ok) {
        const json = await response.json(); /* or .text, etc */
        /* work with the JSON data */
    }
} catch () {
    /* handle errors */
}
1 Like

both ways are not working for me.

here my code:

async function updatecheck(){
    try {
        var connectedOK=false;//optional  
        let httpReq = new XMLHttpRequest();
        let url="http://retouch-panel.de/1.php";
        httpReq.open("GET", url);
        httpReq.send();
        httpReq.onreadystatechange = async function () {
            var serverMessage = await httpReq.responseText;
            connectedOK=true;//optional
            //Do something with server message here    
            showtoast(serverMessage.toString,2000);
        }
        //optional
        setTimeout(function(){ 
            if(connectedOK==false){
            //Do something for connection timeout
            showToast("Timeout",2000);
            } 
        }, 3500);
    }
    catch{
        showToast("fehler",2000);
    }
}

does someone has an idea?

so now I have found a solution. I am using a json array in the PHP file like this:

<?php
header('Content-Type: application/json');
$arr = array('a' => 'hallo', 'b' => 'test', 'c' => 'test2', 'd' => 'test3', 'e' => 'test4');

echo json_encode($arr);
?>

and this code in my function:

    let forecastURL="https://retouch-panel.de/1.php";
    let response = await fetch(forecastURL);
	if (!response.ok) {
		throw new Error(`HTTP error fetching forecast; status: ${response.status}`);
	}

    let resultJson = await response.json();
    console.log('.. got :' + resultJson.b);    
    answer = resultJson.b;
return answer;
1 Like

Not sure why it doesn’t work for you. I use XMLHttpRequest in 8 plugins and it is working in all of mine. I did notice in your code you have showtoast (with small t) in one place and showToast (with capital T) in another. Not sure if this is the issue in your code.

One thing I do notice in one of the samples: http-only web addresses will not work. Both fetch and XMLHttpRequest require secure endpoints unless you’re using your local loopback address.