-1

Forgive me if this is a stupid quesiton but I fell upon an ajax call of type GET that for some reason passed some data along with it. And I thought to myself, who passes data along with a GET request?

$.ajax({
     url: "https://example/api/jobFinished/",
     dataType: "JSONP",
     type: "GET",
     jsonpCallback: "callback",
     data: {
         id: '1852',
         scid: '1952',
         cid: '120125'     
     },

How is data transferred in an ajax GET request?

secondary question: Why would anyone choose to transfer data with an ajax GET request as opposed to an ajax requets of type POST?

Ryan Cameron
  • 361
  • 1
  • 3
  • 12
  • 1
    I don't understand your question. Logically, if you want to GET something from a server, you need to tell it WHAT you want to get; and to do that, you have to pass some data to the server. – Robert Harvey Aug 24 '18 at 14:52
  • It _should_ pass them as query parameters (?id=1852&scid=1952...), as to why you might pass data with a GET is to stick to convention, if you want to _get_ something from a server it'd be silly to use something called POST – George Aug 24 '18 at 14:53
  • `data` on requests that don't support a body (like a GET request) are passed as query strings. It's just a convenience jQuery provides so you can use the same method template for all sorts of requests. – apokryfos Aug 24 '18 at 14:55
  • https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – epascarello Aug 24 '18 at 15:00

1 Answers1

2

How is data transferred in an ajax GET request?

On the query string. For example:

https://example/api/jobFinished/?id=1852&scid=1952&cid=120125

You can observe this in your browser's debugging tools by watching the requests and responses.

Why would anyone choose to transfer data with an ajax GET request

If the server is expecting a GET and not a POST. Conventions suggest that one uses GET when only querying and not modifying data.

David
  • 176,566
  • 33
  • 178
  • 245
  • So bascially that ajax func is basically just converting all the data into a query string and sending that? – Ryan Cameron Aug 24 '18 at 14:55
  • @RyanCameron: Basically, ya. A GET request has no body, only a request to a URL, so that would be where the data has to go. – David Aug 24 '18 at 14:58