passing array of objects from js to rails

前端 未结 2 997
刺人心
刺人心 2020-12-31 15:04

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

data.test = [{test: \'asdas\'}]

$.ajax({
  url: \'evaluate.json\',
  data: data,
  success: functio         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 15:36

    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.

    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.

提交回复
热议问题