[英]Post JSON Data From Javascript To Java
I'm in need of some desperate help. 我需要一些绝望的帮助。 I've been at this for 4 hours, and I'm getting pretty worn out. 我已经待了四个小时了,我已经很累了。 :/ Here's my situation: :/这是我的情况:
I have a Javascript application that is making a POST request (using jQuery $.post) to an external site. 我有一个Javascript应用程序正在向外部站点发出POST请求(使用jQuery $ .post)。 On the external site I have Apache Camel running with Jetty to expose it to the web. 在外部站点上,我让Apache Camel与Jetty一起运行以将其公开。 The web services I wrote in Camel expect JSON data for all of the requests. 我在Camel中编写的Web服务期望所有请求都具有JSON数据。 For instance, one request needs an id, so I send it {"id": 10}
. 例如,一个请求需要一个id,因此我将其发送给{"id": 10}
。
Here's my issue: it doesn't work from Javascript. 这是我的问题:它不适用于Javascript。 I have a few different tools that will send post requests for me (like the Poster extension for browsers). 我有几种不同的工具可以为我发送帖子请求(例如浏览器的Poster扩展)。 If I use Poster and set the body to {"id": 10}
, it works just fine. 如果我使用Poster并将body设置为{"id": 10}
,则效果很好。 I get that exact string in the service. 我在服务中得到了确切的字符串。
But, if I post from Javascript, I get something different. 但是,如果我从Javascript发帖,我会有所不同。 Posting the JSON object will give me the string "id=10" on my service side. 发布JSON对象将在服务端给我字符串“ id = 10”。 (It's OK for this scenario, but I will need actual JSON objects eventually.) If I stringify the JSON object, I get the JSON string, only all of the characters are escaped. (在这种情况下可以,但是最终我将需要实际的JSON对象。)如果我将JSON对象字符串化,则会得到JSON字符串,只有所有字符都被转义。 (Ex. "%7Bid%33..."). (例如“%7Bid%33 ...”)。
I swear I've tried every method possible for posting the data, but I either get the weird already parsed JSON, or the escaped string (or nothing at all). 我发誓我尝试了所有可能的方法来发布数据,但是我要么得到了已经解析过的怪异的JSON,要么得到了转义的字符串(或根本没有)。 Is there some way I can have Javascript NOT parse the JSON object and just send it (like my posting tool does)? 有什么方法可以让Javascript不解析JSON对象而只发送它(就像我的发布工具一样)? If not, is there a safe, efficient way to un-escape the JSON string that I get? 如果没有,是否存在一种安全,有效的方法来转义我得到的JSON字符串?
I really appreciate any help. 我非常感谢您的帮助。
I feel like we need a little bit more information, but take a look at this javascript plugin. 我觉得我们需要更多信息,但请看一下此javascript插件。 It may be your solution: https://github.com/flowersinthesand/jquery-stringifyJSON 可能是您的解决方案: https : //github.com/flowersinthesand/jquery-stringifyJSON
Try using jQuery.ajax and setting processData
to false
(defaults to true
): 尝试使用jQuery.ajax并将processData
设置为false
(默认为true
):
$.ajax({
url: '/where/to/post',
type: 'POST',
data: {"id": 10},
processData: false
});
Usually, jQuery converts anything in data
to query string format like id=10
. 通常,jQuery将data
任何内容转换为查询字符串格式,例如id=10
。 The processData
flag tells jQuery to interpret it literally as a json hashmap. processData
标志告诉jQuery将其字面解释为json哈希图。
Posting the JSON object will give me the string "id=10" on my service side. 发布JSON对象将在服务端给我字符串“ id = 10”。
Javascript does not do your this conversion, so your server does it. JavaScript不会执行此转换,因此服务器会执行此转换。
It is likely that your server reacts differently based on the content-type
of your POST eg application/json
vs text/plain
or text/html
, a common feature of REST based services. 服务器可能根据POST的content-type
做出不同的反应,例如application/json
与text/plain
或text/html
(这是基于REST的服务的常见功能)。
The answers here gave me a few hints, but ultimately, it was a lot of tweaking before it would work correctly. 这里的答案给了我一些提示,但是最终,它需要大量的调整才能正常工作。 I had to do 3 things: 我必须做三件事:
processData: false
. 添加processData: false
。 contentType
to application/json
). 如果我将其保留为对象(即使将contentType
更改为application/json
),该请求也不会触发。 contentType to
text/plain
. 将contentType to
更改contentType to
text/plain
。 This sent it as a raw string. 这将其作为原始字符串发送。 And that's what did the trick. 这就是窍门。 I now get the JSON string I want on the server side. 现在,我在服务器端获取了想要的JSON字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.