英文:
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: '',
headers: {
"listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
},
dataType: 'json',
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: "application/json"
as an object property. See the contentType
property in the docs.
<strong>Full Example:</strong>
...
$.ajax({
url: '',
contentType: 'application/json',
headers: {
"listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
},
dataType: 'json',
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: 'application/json',
accept: 'application/json',
url: '',
headers: {
"listkey":{"key1":"val1", "key2": "val2", "key3":"val3"}
},
dataType: 'json',
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: 'application/json',
accept: 'application/json',
url: '',
headers: {
"listkey": '{"key1":"val1", "key2": "val2", "key3":"val3"}'
},
dataType: 'json',
cache: false,
success: function(data) {
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论