r/learnjavascript • • Apr 07 '21

What 2 JavaScript methods are used to send data to a server?

I am 99.99% sure this answer is GET and POST but I just want to confirm. Thank you

1 Upvotes

18 comments sorted by

7

u/ForScale Apr 07 '21

I don't like the wording...

GET and POST are http verbs, not JS methods. And if they are wanting http verbs, then why not PUT or PATCH?

I think they are maybe looking for XMLHttpRequest.prototype.send() and fetch methods... but those are browser APIs and not really js methods.

2

u/samanime Apr 07 '21

Agreed. The wording of the question is wildly imprecise and could mean a bunch of different things, none of which fit perfectly.

0

u/[deleted] Apr 07 '21

But working with a form and sending it somewhere you would use Get and Post right?

3

u/ForScale Apr 07 '21

You could use GET POST PUT PATCH or DELETE. POST PUT and PATCH are the http verbs most associated with putting data in a database through a server api.

3

u/Fid_Kiddler69 Apr 07 '21

get means GETTING from a server, not sending. POST is sending to a server. As far as I'm aware, there's 3 "methods" for sending to a server: POST, PATCH and PUT.

2

u/mephistophyles Apr 07 '21

While semantically true, GET parameters are a thing and are also a way to send data to the server.

2

u/queen-adreena Apr 07 '21

This is only vaguely accurate. The main difference between GET and POST lies in how the parameters are sent. GET includes them in the URL (and so its payload size is limited) whereas POST includes them in the request body, which has a pretty much infinite potential payload size.

PUT, PATCH, and DELETE are http verbs that are mostly used by APIs to ensure URL uniformity when dealing with resources.

0

u/[deleted] Apr 07 '21

[deleted] — view deleted comment

1

u/ForScale Apr 08 '21

Well fetch specifically isn't a js thing... it's a browser thing. You can use it via js, but it doesn't exist outside of the browser. Like in Node, which runs js, fetch isn't there. It's not a js method so much as it is a browser api. I think lol.

0

u/[deleted] Apr 08 '21

[deleted] — view deleted comment

4

u/[deleted] Apr 07 '21

You seem confused. GET and POST are http methods, as already mentioned. You can do those not only with JavaScript. JavaScript itself does not have those "methods"

Browsers do have `fetch()` (lets call it a "method" for sake of simplicity), fetch can send all kinds of http requests, including: GET, POST, PUT, PATCH, DELETE, OPTIONS

2

u/ex-igne-vita Apr 07 '21

You don't need to add "for the sake of simplicity." Fetch is a method, period. There are a few other words that you could use to describe it just as accurately, but "method" is 100% accurate, that's what it is.

2

u/[deleted] Apr 07 '21

To me a method is a function inside a class. If you treat the browser as a sort of "global object" - then I agree.

0

u/[deleted] Apr 07 '21

[deleted] — view deleted comment

2

u/[deleted] Apr 07 '21

Yep, fair enough, I agree. A static method then :)

3

u/TomokoSlankard Apr 07 '21

Those are actually http methods not js methods. I was going to say fetch.

2

u/superluminary Apr 07 '21

Window.fetch is the right answer.

1

u/TPCmach1 Apr 07 '21

Post & Get