XMLHttpRequest() and fetch() in Javascript(let) returns nothing sometimes
I would first like to acknowledge that I might be getting something absolutely wrong here.
The code below will have the following results in chrome (accounted for CORS):
- results[0-8].fetch = "..." // contains something
- results[0-8].xhttp = "..."
but will have the following results in Tasker:
- results[0-4].fetch = "" // undefined
- results[0-4].xhttp = ""
- results[5-8].fetch = "..."
- results[5-8].xhttp = "..."
var results = [];
var notWorkingLinks = [
"http://wifij01us.magichue.net/app/sendRequestCommand/ZG001",
"http://wifij01us.magichue.net",
"https://fcklinks.fr",
"https://fcklinks.fr/index.html",
"https://google.com"
];
var workingLinks = [
"https://fcklinks.fr/php.php",
"https://fcklinks.fr/css.css",
"https://fcklinks.fr/js.js",
"https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"
]
notWorkingLinks.forEach((url) => {
results.push(get(url));
});
workingLinks.forEach((url) => {
results.push(get(url));
});
function get(url) {
var result = {};
fetch(url, {
method: "GET"
}).then(async (response) => {
if (response) {
await response.text().then((r) => {
result["fetch"] = r;
});
}
});
let xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.onload = () => {
if (xhttp.response) {
result["xhttp"] = xhttp.responseText;
}
};
xhttp.send();
return result;
}
Few things to note:
- An observation will be that something is only returned if the URL doesn't end with "html" or a sever directory.
- The results here are the same with a POST request too.
- I currently have no workaround other than to use a plugin or create a php server that requests info then relays it onto the page.
- It doesn't seem to be a special character issue as it works with the js, css, and php files.
- The request seems to still go through successfully, just without a response.
- I'm running Tasker 5.7.2 on Andoid 9.0, with May security patches, on a Galaxy S10, without root