mrowlsss @mrowlsss

can someone PLEASE give me some vanilla javascript code that will make a get request to https://api.wasteof.money/posts/64fa845c62c9b104f65962d4/ and display it as an <p> element on screen?

Sep 8, 2023, 3:17 AM
15

comments

fetch("https://api.wasteof.money/posts/64fa845c62c9b104f65962d4/").then(r => r.json()).then(j => document.querySelector("#the_element").innerHTML = j.content).catch(e => document.querySelector("#the_element").innerHTML = `<p>error: ${e}</p>`)

hey im dumb how do i make #the_element? do i add “class=’#the_element’”?

no, # is for id (i.e., id="the_element). classes begin with . as opposed to #

oh ok i kinda forget some things after 3 months of not programming

TypeError: Failed to fetch

can you change the catch part to

.catch(e => {
  console.dir(e);
  document.querySelector("#the_element").innerHTML = `<p>error: ${e}</p>`;
});

and then tell me what’s in the console?

what jeffalo said, i suck with apis :P

currently not on my computer, but essentially you have to use fetch to get the content of https://api.wasteof.money/posts/64fa845c62c9b104f65962d4/, and then set the .innerHTML of an element to the post.content

here’s an example of it: https://git.tnix.dev/Tnix/random/src/branch/main/wasteof/example-64fa922ea5efdaee65509cce.html

btw the API has CORS, so requests to the API from another website will fail, you’d have to use something like a browser extension to bypass CORS policies, but I don’t recommend that for security reasons.

adding on to what tnix’s response was, you could replace the url in the JS with a corsproxy version of it with no need for an extension.

oh yes, I forgot those existed

oh, thanks. If you have time later, can you provide a code snippet for making the fetch request? I can do the inter html part

I would but I don't have my computer on this trip. @jeffalo can you help them, you definitely know how to do it