0

I have seen that it's possible to set the content type of a Ajax POST request to json. From what I understand this means that the data send (which you should stringify first) will be sent as json in the body of he request.

Is it therefore a mistake to set the content type to json when doing an Ajax GET request? Can you send json as part of the url query string?

Much thanks

bobbo
  • 845
  • 2
  • 14
  • 24
  • 1
    [Related](http://stackoverflow.com/questions/17725110/jquery-ajax-get-and-contenttype). When you're requesting something via GET, the body is empty, and the content type refers to the data type of the body. – Dave Chen Dec 18 '15 at 06:56
  • You can still send data to a GET request using for example `data: { id: someValue }`. There is no need to stringify it, but if you do (using `data: JSON.stringify({ id: someValue })`) then you do need to set the `contentType` to 'json' –  Dec 18 '15 at 06:57
  • @StephenMuecke you comment about setting the content type to json seems to contract David's comment. – bobbo Dec 18 '15 at 08:15
  • No it doesn't. I'm saying if you do send data using the `data:` option and you stringify it, then you can set the `contentType` option. But your claim that _you should stringify first_ is wrong - you do not need to stringify it. –  Dec 18 '15 at 08:17
  • With a POST can I ask what is the purpose of setting the content type to json (e.g. contentType: "application/json") and not using the stringify? How will the data be transmitted? What is the advantage of using JSON.stringify? – bobbo Dec 18 '15 at 08:27

0 Answers0