导轨没有jQuery的正确解码JSON(阵列成为整数键的哈希) [英] Rails not decoding JSON from jQuery correctly (array becoming a hash with integer keys)

查看:127
本文介绍了导轨没有jQuery的正确解码JSON(阵列成为整数键的哈希)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我要发布使用jQuery JSON对象数组到Rails的每一次,我有这个问题。
如果我字符串化数组我可以看到,jQuery是做正确的工作:


every time I want to post an array of JSON objects with jQuery to Rails, I have this problem. If I stringify the array I can see that jQuery is doing its work correctly:

\"shared_items\"=>\"[{\\\"entity_id\\\":\\\"253\\\",\\\"position\\\":1},{\\\"entity_id\\\":\\\"823\\\",\\\"position\\\":2}]\"

"shared_items"=>"[{\"entity_id\":\"253\",\"position\":1},{\"entity_id\":\"823\",\"position\":2}]"

但是,如果我只是把数组作为Ajax调用的数据我得到:

But if I just send the array it as the data of the ajax call I get:

shared_items=> {0=> {ENTITY_ID=>253,位置=>1},1=> {ENTITY_ID=>823,位置=>2}}

"shared_items"=>{"0"=>{"entity_id"=>"253", "position"=>"1"}, "1"=>{"entity_id"=>"823", "position"=>"2"}}

然而,如果我只是发送纯数组它的工作原理:

Whereas if I just send a plain array it works:

shared_items=> [entity_253]

"shared_items"=>["entity_253"]

为什么Rails的改变阵列那个奇怪的哈希?该想到的唯一原因是的Rails不能正确理解的内容,因为没有此类型(有没有办法将其设置在jQuery的电话吗?)

Why is Rails changing the array to that strange hash? The only reason that comes to mind is that Rails can't correctly understand the contents because there is no type here (is there a way to set it in the jQuery call?):

处理由SharedListsController#创建为

Processing by SharedListsController#create as

感谢您!

更新:
我将数据作为一个数组,而不是字符串和数组创建动态使用.push()函数。试着用$。员额和$就,同样的结果。

Update: I'm sending the data as an array, not a string and the array is created dynamically using the .push() function. Tried with $.post and $.ajax, same result.

推荐答案

在万一有人绊倒在此,希望更好的解决方案,您可以指定的contentType:应用/ JSON',在阿贾克斯看涨期权和有导轨正确解析JSON对象,而所有的字符串值错乱它变成整数键控散列。

In case someone stumbles upon this and wants a better solution, you can specify the "contentType: 'application/json'" option in the .ajax call and have Rails properly parse the JSON object without garbling it into integer-keyed hashes with all-string values.

因此​​,要总结,我的问题是,这:

So, to summarize, my problem was that this:

$.ajax({
  type : "POST",
  url :  'http://localhost:3001/plugin/bulk_import/',
  dataType: 'json',
  data : {"shared_items": [{"entity_id":"253","position":1}, {"entity_id":"823","position":2}]}
});

造成了Rails的解析事情:

resulted in Rails parsing things as:

Parameters: {"shared_items"=>{"0"=>{"entity_id"=>"253", "position"=>"1"}, "1"=>{"entity_id"=>"823", "position"=>"2"}}}

而这个(注:我们现在字符串化的JavaScript对象,并指定内容类型,所以Rails会知道如何解析我们的字符串):

whereas this (NOTE: we're now stringifying the javascript object and specifying a content type, so rails will know how to parse our string):

$.ajax({
  type : "POST",
  url :  'http://localhost:3001/plugin/bulk_import/',
  dataType: 'json',
  contentType: 'application/json',
  data : JSON.stringify({"shared_items": [{"entity_id":"253","position":1}, {"entity_id":"823","position":2}]})
});

结果在Rails的一个不错的对象:

results in a nice object in Rails:

Parameters: {"shared_items"=>[{"entity_id"=>"253", "position"=>1}, {"entity_id"=>"823", "position"=>2}]}

这对我的作品在Rails 3中,在红宝石1.9.3。

This works for me in Rails 3, on Ruby 1.9.3.

这篇关于导轨没有jQuery的正确解码JSON(阵列成为整数键的哈希)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆