Syl Syl - 2 months ago 6
jQuery Question

How do i read a JSON sent by ajax in rails?

I am sending a JS array to my rails app :

$.ajax({
url: "/backend/familles/change_order.js",
contentType: 'json',
type: "POST",
data: JSON.stringify(sort_array)
});


Here is how firefox see the ajax POST :

http://i.imgur.com/wdy2MJa.png

But a params.inspect only contain the following :

{"controller"=>"backend/familles", "action"=>"change_order", "locale"=>"fr"}


What should I change to be able to read the JSON data?

Answer

There is no need to explicitly stringify your data object, jQuery will do that for you! Also you should add a key value for the data you wish to send. Something like this should work:

data: {"sort_array": sort_array}
Comments