将对象数组从js传递到rails

编程入门 行业动态 更新时间:2024-10-25 03:21:47
本文介绍了将对象数组从js传递到rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将对象数组从js传递到rails

I am trying to pass an array of objects from js to rails

data.test = [{test: 'asdas'}] $.ajax({ url: 'evaluate.json', data: data, success: function(data){ }, dataType : "json" });

铁路

def evaluate logger.info("#{params.test}") end

在这里,记录器语句总是让我失望

Here the logger statement always gives me out put as

{"0"=>{"test"=>"asdas"}}

我期望下面的登录方式.

I am expecting the below log in rails.

[{:test=>"asdas"}]

推荐答案

您应该在Javascript中使用JSON.stringify,它使用数组或哈希作为参数(因为这是唯一有效的JSON构造).它返回一个表单,该表单是序列化为JSON的Javascript对象.

You should use JSON.stringify in Javascript, which takes either an array or hash as its argument (since these are the only valid JSON constructions). It returns a form which is the Javascript object serialized to JSON.

在Ruby端,您将收到一个JSON编码的字符串,因此您需要require 'json'(在Rails中自动完成)并使用JSON.parse(string).这将为您提供一个Ruby对象.

On the Ruby side, you'll receive a JSON encoded string, so you'll need to require 'json' (this is done automatically in Rails) and use JSON.parse(string). This will give you a Ruby object.

更多推荐

将对象数组从js传递到rails

本文发布于:2023-10-07 17:34:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   rails   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!