问题描述
因此根据 jQuery的Ajax的文档,它发送请求时串行数据在查询字符串的形式,但设置过程数据:假
应该让我的身体发出的实际JSON。不幸的是,我有一个很难确定的第一,如果发生这种情况,并第二次是什么物体看起来像正在被发送到服务器。我所知道的是,服务器并不解析什么我发送。
使用 HTTP客户端发布对象文本时 {someKey:someData'}
它的工作原理。但使用jQuery与数据:{someKey:someData'}
失败。不幸的是,当我分析了Safari浏览器的请求时,它表示该消息负载是 [对象的对象]
...伟大的...而在Firefox后是空白...
在登录的主体内容在Java端,它确实获得 [对象的对象]
那么,如何发送实时JSON数据??
有没有人有一个Java服务序列化JSON数据请求体,与jQuery的发送请求的经验?
BTW这里是全$就要求:
$。阿贾克斯({
的contentType:应用/ JSON的,
数据: {
命令:上
},
数据类型:JSON,
成功:功能(数据){
app.log(设备控制成功);
},
错误:函数(){
app.log(设备控制失败);
},
过程数据:假的,
键入:POST,
网址:'/设备/ {DEVICE_ID} /控制
});
这是实际的JSON请求是这样的:
数据:{命令:关于}
如果你发送一个实际的JSON字符串。对于更广泛的解决方案,使用<$c$c>JSON.stringify()$c$c>序列化对象到JSON,像这样的:
数据:JSON.stringify({命令:关于}),
要支持不具备 JSON
对象旧的浏览器,使用 json2.js 将在添加。
什么是目前发生的事情是,因为你有过程数据:假
,它基本上发送此:({命令:关于})。的toString()
是 [对象的对象]
...你在你的请求看到的。
So according to the jQuery Ajax docs, it serializes data in the form of a query string when sending requests, but setting processData:false
should allow me to send actual JSON in the body. Unfortunately I'm having a hard time determining first, if this is happening and 2nd what the object looks like that is being sent to the server. All I know is that the server is not parsing what I'm sending.
Using http client when posting an object literal {someKey:'someData'}
it works. but using jQuery with data: {someKey:'someData'}
it fails. Unfortunately when i analyze the request in safari, it says the message payload is [object Object]
... great... and in Firefox the post is blank...
When logging the body content on the java side it literally gets [object Object]
so how does one send REAL json data??
Has anyone had experience with a Java service serializing json data in the request body, with the request sent from jQuery?
BTW here is the full $.ajax request:
$.ajax({
contentType: 'application/json',
data: {
"command": "on"
},
dataType: 'json',
success: function(data){
app.log("device control succeeded");
},
error: function(){
app.log("Device control failed");
},
processData: false,
type: 'POST',
url: '/devices/{device_id}/control'
});
An actual JSON request would look like this:
data: '{"command":"on"}',
Where you're sending an actual JSON string. For a more general solution, use JSON.stringify()
to serialize an object to JSON, like this:
data: JSON.stringify({ "command": "on" }),
To support older browsers that don't have the JSON
object, use json2.js which will add it in.
What's currently happening is since you have processData: false
, it's basically sending this: ({"command":"on"}).toString()
which is [object Object]
...what you see in your request.
这篇关于jQuery的张贴在请求主体有效的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!