passing array of objects from js to rails

此生再无相见时 提交于 2019-12-03 14:54:58

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.

Try this:

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

$.ajax({
  url: 'evaluate.json',
  data: JSON.stringify(data),  // Explicit JSON serialization
  contentType: 'application/json',  // Overwrite the default content type: application/x-www-form-urlencoded
  success: function(data){
  },
  dataType : "json"
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!