owl — 9/8/2023, 3:17:02 AM

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?

♥ 4 ↩ 0 💬 15 comments

comments

lily:
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>`)
9/8/2023, 2:23:51 PM
owl:

thanks

9/8/2023, 2:28:48 PM
owl:

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

9/8/2023, 2:40:18 PM
lily:

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

9/8/2023, 7:04:26 PM
owl:

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

9/8/2023, 10:07:20 PM
owl:

TypeError: Failed to fetch

9/9/2023, 5:14:48 PM
lily:

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?

9/10/2023, 10:15:01 AM
owl:

ok i will

9/11/2023, 7:27:15 PM
3xiondev:

what jeffalo said, i suck with apis :P

9/8/2023, 10:18:31 AM
jeffalo:

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

9/8/2023, 4:14:27 AM
tnix:

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.

9/8/2023, 5:21:50 AM
radi8:

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.

9/8/2023, 11:59:59 AM
tnix:

oh yes, I forgot those existed

9/8/2023, 1:46:45 PM
owl:

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

9/8/2023, 2:04:04 PM
oren:

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

9/8/2023, 3:34:25 AM