using rails 2.3.8.
I’m getting this:
[
[0] "0",
[1] {
"name" => "Section",
"contents" => {
"0" => {
"name" => "RENAMED!",
"id" => "1"
}
}
}
]
notice the [0] and the [1]
but in my post, I formatted the JSON like this:
[ {
"name":"Section",
"contents":[
{
"id":1,
"name":"RENAMED!"
}
]
}, .. more of this type of structure [
so, why is rails adding the additional array?
Here is how I’m posting the object:
$j.ajax({
type: "POST",
url: 'http://url/objects/create/',
dataType: 'text',
async: false,
data: data_obj,
success: function () {
alert("sent");
}
});
UPDATE
data_obj = {
"my_object":{
"name":"hello there, I am JSON!",
"template_id":1,
"variables":{
"hello":"there",
"me":"you"
},
"sections":[
{
"name":"Section",
"contents":[
{
"id":1,
"name":"RENAMED!"
}
]
},
{
"name":"section2",
"contents":[
{
"name":"something",
"body":"nothing"
},
{
"id":2,
"name":"I renamed you",
"variables":{
"hello":"i'm amazing"
}
}
]
}
],
"attachments":[
{
"media_id":1
}
]
}
}
Rails is not adding the extra element to the json array. That’s happening on the browser.
Check your javascript carefully for an “edge case” of adding an empty element to the array. Eg how are you initializing the array.
If you’re not convinced by the above on where the bug is, add a breakpoint before the ajax call and examine the
data_objarray.ps. it should be an array, but you’ve named it
data_obj. Are you treating it as an object at some point? Are you setting the first element to be at index 1? (That would be a bug.) You should onlypushvalues onto the array.ADDED I think the problem is that you’re not sending the data as json.
The default for sending data to the server is key/value pairs.
Try
See post. You may also have to set the mime-type. The post shows how.
alt for the structure: