Syl
Syl

Reputation: 3829

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?

Upvotes: 0

Views: 55

Answers (2)

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

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}

Upvotes: 1

sandro d.
sandro d.

Reputation: 156

you should use

dataType: 'json'

not contentType

EDIT:

I think you should not send an Array directly. Try:

data: JSON.stringify({"items":sort_array})

or

data: {"items": JSON.stringify(sort_array)}

Upvotes: 1

Related Questions