Syl Syl - 10 months ago 45
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 Source

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}