每次我想将带有jQuery的JSON对象数组发布到Rails时,我都会遇到这个问题.
"shared_items"=>"[{\"entity_id\":\"253\",\"position\":1},{\"entity_id\":\"823\",\"position\":2}]"
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调用中设置它?):
Processing by SharedListsController#create as
Thank you!
Update:
我将数据作为数组发送,而不是字符串,并且该数组是使用.push()
函数动态创建的.try 使用$.post
和$.ajax
,结果相同.