0

How do you set a json header in query. I need it to be a string on the server?:

...
$.ajax({
  url: '',
  headers: {
    "listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
  },
  dataType: 'json',
  cache: false,
  success: function(data) {
...
Chris G.
  • 23,930
  • 48
  • 177
  • 302

3 Answers3

1

I believe it's a simple as adding:

contentType: "application/json"

as an object property. See the contentType property in the docs.

Full Example:

...
$.ajax({
    url: '',
    contentType: 'application/json',
    headers: {
      "listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
    },
    dataType: 'json',
    cache: false,
    success: function(data) {
...
War10ck
  • 12,387
  • 7
  • 41
  • 54
1

you can use the contentType property to set the content-type header, i.e. what you are sending to the server.

And you can use the accept property to tell the server what you would like back.

$.ajax({
  contentType: 'application/json',
  accept: 'application/json',
  url: '',
  headers: {
    "listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
  },
  dataType: 'json',
  cache: false,
  success: function(data) {
...
Sam
  • 895
  • 1
  • 8
  • 26
0

This worked:

$.ajax({
  contentType: 'application/json',
  accept: 'application/json',
  url: '',
  headers: {
    "listkey": '{"key1":"val1", "key2": "val2", "key3":"val3"}'
  },
  dataType: 'json',
  cache: false,
  success: function(data) {
...
Chris G.
  • 23,930
  • 48
  • 177
  • 302