Ask coding questions

← Back to all posts
How do I do a GET request in Javascript
Mathman05 (1)

I'm trying to do a get request in javascript to but nothing seems to work, in node.js I found a way to get it to work but in javascript, it always gives me an error. rn it's just {"body":number} (number is a random number between 1 and 0 for testing purposes)

Coder100 (18167)

fetch is not natively supported by node, you have to install it

const fetch = require("node-fetch");
fetch("").then(r => r.json()).then(console.log);


Mathman05 (1)

@Coder100 I said that I got it working in node.js I just can't get it working in javascript.

Coder100 (18167)

js = nodejs ?????

anyways make sure you have cors enabled in the server:

Mathman05 (1)

@Coder100 Error: TypeError: Failed to fetch that just gives an error

xxpertHacker (930)

@Mathman05 what is the error's message?

Mathman05 (1)

@Coder100 yes it gives a different error if it's not

Mathman05 (1)

@xxpertHacker Error: TypeError: Failed to fetch

Mathman05 (1)

@Coder100 I'd also made it in node.js it works fine there but in normal js it just errors with the error I mentioned earlier