mutidimensional array from javascript/jquery to ruby/sinatra

Posted by user199368 on Stack Overflow See other posts from Stack Overflow or by user199368
Published on 2009-10-30T02:15:57Z Indexed on 2010/03/21 0:01 UTC
Read the original article Hit count: 413

Hi,

how do I pass a 2-dimensional array from javascript to ruby, please? I have this on client side:

function send_data() {
    var testdata = {
        "1": {
            "name": "client_1",
            "note": "bigboy"
        },
        "2": {
            "name": "client_2",
            "note": "smallboy"
        }
    }

    console.log(testdata);
    $.ajax({
      type: 'POST',
      url: 'test',
      dataType: 'json',
      data: testdata
    });
  }

and this on server side:

post '/test' do p params end

but I can't get it right. The best I could get on server side is something like

{"1"=>"[object Object]", "2"=>"[object Object]"}

I tried to add JSON.stringify on client side and JSON.parse on server side, but the first resulted in

{"{\"1\":{\"name\":\"client_1\",\"note\":\"bigboy\"},\"2\":{\"name\":\"client_2\",\"note\":\"smallboy\"}}"=>nil}

while the latter has thrown a TypeError - can't convert Hash into String.

Could anyone help, or maybe post a short snippet of correct code, please? Thank you

© Stack Overflow or respective owner

Related posts about ruby

Related posts about sinatra