JSON格式(通过jQuery AJAX的职位为Java /检票服务器发送JSON) [英] JSON formatting (Sending JSON via jQuery AJAX post to Java/Wicket server)
问题描述
我使用jQuery张贴JSON一个Java服务器,但我想我的JSON一定是错误的。下面是我的数据为例,我如何将其发送:
I'm using jQuery to post JSON to a Java server, but I think my JSON must be wrong. Here's an example of my data and how I'm sending it:
var lookup = {
'name': name,
'description': description,
'items': [{
'name': itemName,
'value': itemValue
}]
}
$.ajax({
type: 'post',
data: lookup,
dataType: 'json'
});
我使用的是检票的AbstractAjaxBehavior接收数据,并希望得到一个JSON字符串,我可以解析。当我得到一个Map传递的参数,键集是这样的:
I'm using Wicket's AbstractAjaxBehavior to receive the data and would like to get a single JSON string that I can parse. When I get a Map of the parameters passed, the keyset looks like this:
items[0][name],
description,
name,
items[0][value],
很显然,我可以轻松地获得了名称和描述的值,但关键的我的项目的数组搞砸。我敢肯定,这是简单的东西,但我似乎让周围的解决方案运行。有什么建议么?谢谢!
Obviously I can easily get the values for name and description, but the key for my array of items is messed up. I'm sure it's something simple, but I seem to keep running around the solution. Any suggestions? Thanks!
推荐答案
您必须使用JSON.stringify:
You have to use JSON.stringify:
$.ajax({
type: 'post',
data: JSON.stringify(lookup),
contentType: 'application/json',
dataType: 'json'
});
您也应该指定应用/ JSON'作为的contentType。默认情况下jQuery将与序列化应用程序/ x-WWW的形式urlen codeD对象(即使contentType为application / JSON)。所以,你必须做手工。
You should also specify 'application/json' as the contentType. By default jQuery will serialize objects with application/x-www-form-urlencoded (even if the contentType is application/json'). So you have to do it manually.
编辑:密钥'后'应该是类型,而不是方法
Key for 'post' should be type, not method.
这篇关于JSON格式(通过jQuery AJAX的职位为Java /检票服务器发送JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!