你好!以下是你要翻译的内容: jquery和go:如何设置JSON头部。

huangapple go评论73阅读模式
英文:

jquery & go : how do you set a json header

问题

如何在查询中设置 JSON 标头?我需要它在服务器上是一个字符串。

...
$.ajax({
  url: '',
  headers: {
    "listkey": "{\"key1\":\"val1\", \"key2\": \"val2\", \"key3\":\"val3\"}"
  },
  dataType: 'json',
  cache: false,
  success: function(data) {
...

在这个例子中,我们使用 headers 属性来设置 JSON 标头。在 headers 对象中,我们将 "listkey" 键的值设置为一个 JSON 字符串。请注意,JSON 字符串需要使用双引号 " 包裹键和值。

英文:

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

...
$.ajax({
  url: &#39;&#39;,
  headers: {
    &quot;listkey&quot;:{&quot;key1&quot;:&quot;val1&quot;, &quot;key2&quot;: &quot;val2&quot;, &quot;key3&quot;:&quot;val3&quot;}
  },
  dataType: &#39;json&#39;,
  cache: false,
  success: function(data) {
...

答案1

得分: 1

我相信只需要添加以下代码即可:

contentType: "application/json"

作为对象属性。请参考文档中的contentType属性。

完整示例:

...
$.ajax({
    url: '',
    contentType: 'application/json',
    headers: {
      "listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
    },
    dataType: 'json',
    cache: false,
    success: function(data) {
...
英文:

I believe it's a simple as adding:

contentType: &quot;application/json&quot;

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

<strong>Full Example:</strong>

...
$.ajax({
    url: &#39;&#39;,
    contentType: &#39;application/json&#39;,
    headers: {
      &quot;listkey&quot;:{&quot;key1&quot;:&quot;val1&quot;, &quot;key2&quot;: &quot;val2&quot;, &quot;key3&quot;:&quot;val3&quot;}
    },
    dataType: &#39;json&#39;,
    cache: false,
    success: function(data) {
...

答案2

得分: 1

你可以使用contentType属性来设置content-type头部,即你发送给服务器的内容。

而你可以使用accept属性告诉服务器你希望得到什么返回结果。

$.ajax({
  contentType: 'application/json',
  accept: 'application/json',
  url: '',
  headers: {
    "listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
  },
  dataType: 'json',
  cache: false,
  success: function(data) {
    ...
  }
});
英文:

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: &#39;application/json&#39;,
  accept: &#39;application/json&#39;,
  url: &#39;&#39;,
  headers: {
    &quot;listkey&quot;:{&quot;key1&quot;:&quot;val1&quot;, &quot;key2&quot;: &quot;val2&quot;, &quot;key3&quot;:&quot;val3&quot;}
  },
  dataType: &#39;json&#39;,
  cache: false,
  success: function(data) {
...

答案3

得分: 0

这个代码是有效的:

$.ajax({
  contentType: 'application/json',
  accept: 'application/json',
  url: '',
  headers: {
    "listkey": '{"key1":"val1", "key2": "val2", "key3":"val3"}'
  },
  dataType: 'json',
  cache: false,
  success: function(data) {
...
英文:

This worked:

$.ajax({
  contentType: &#39;application/json&#39;,
  accept: &#39;application/json&#39;,
  url: &#39;&#39;,
  headers: {
    &quot;listkey&quot;: &#39;{&quot;key1&quot;:&quot;val1&quot;, &quot;key2&quot;: &quot;val2&quot;, &quot;key3&quot;:&quot;val3&quot;}&#39;
  },
  dataType: &#39;json&#39;,
  cache: false,
  success: function(data) {
...

huangapple
  • 本文由 发表于 2015年5月7日 21:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/30102045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定