Receiving information from outside of microstudio
When I first attempted doing this I tried using the fetch() method in JavaScript, but it seemed to not work and now I'm stumped.
My question is: is it possible to receive data from other websites?
solved.
answer: fetch("https://api.ipify.org/").then(async response => print(await response.text()));
How did you use the Fetch API? It should work if the site's CORS policy allows cross-origin requests.
I keep getting thrown an error 404 even though the website is valid and the CORS policy allows cross origin requests....
fetch("en.wikipedia.org/wiki/Memento_mori")
.then(response => {
if (!response.ok) {
print(`http Er! -- status: ${response.status}`);
}
return response.json();
})
.then(data => {
print(data);
})
.catch(error => {
print('Er! -- ' + error);
});
The reason why it doesn't work is because you miss "https://" in front of your address. Otherwise it will not work.
AFAIK however, it seems that either microstudio or wikipedia are blocking CORS.
When I first tried using 'fetch()' I did include the 'https://' in the url and it still seemed to throw errors. It's working now though.
Thanks :-)
edit - I take it back, no website seems to be able to fetch information? Even ones that I verified to allow cross origin requests.
Are there any other methods of fetching information/data from websites? preferably in JavaScript but other languages are fine.
For me, something like this works fine in JavaScript:
fetch("https://api.ipify.org/").then(async response => print(await response.text()));
Maybe your issue is because you're using json()
on an HTML response?
turns out every website I tried were blocking CORS.
and you're right, I should have been using a text response for the type of data I was trying to fetch.