Rails 没有正确解码来自 jQuery 的 JSON(数组变成带有整数键的散列) [英] Rails not decoding JSON from jQuery correctly (array becoming a hash with integer keys)

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

问题描述

每次我想用 jQuery 向 Rails POST 一组 JSON 对象时,我都会遇到这个问题.如果我对数组进行字符串化,我可以看到 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}]"

但是如果我只是将数组作为 AJAX 调用的数据发送,我会得到:

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

"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"]

为什么 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?):

Processing by SharedListsController#create as 

谢谢!

更新:我将数据作为数组而不是字符串发送,并且该数组是使用 .push() 函数动态创建的.尝试使用 $.post$.ajax,结果相同.

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.

推荐答案

如果有人偶然发现并想要更好的解决方案,您可以在 .ajax 调用中指定contentType: 'application/json'"选项并有Rails 正确解析 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}]}

这在 Ruby 1.9.3 上的 Rails 3 中对我有用.

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

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

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