# AJAX: send JSONified ruby-array back to client problem

**URL:** <https://discuss.rubyonrails.org/t/ajax-send-jsonified-ruby-array-back-to-client-problem/22567>\
**Category:** rubyonrails-talk\
**Created:** [January 2, 2008, 8:15pm UTC](https://discuss.rubyonrails.org/t/ajax-send-jsonified-ruby-array-back-to-client-problem/22567 "2008-01-02T20:15:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [January 2, 2008, 8:15pm UTC](https://discuss.rubyonrails.org/t/ajax-send-jsonified-ruby-array-back-to-client-problem/22567/1 "2008-01-02T20:15:40Z")

</div>

Hi All

I'm trying to send a ruby-array back to a browser (its an AJAX call and I'm converting the array using to\_json)

Here is the controller/action part:

respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...... &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.js { # AJAX call &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = [{}] # an array with an hash &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[0]['xyz'] = 'abc' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[1] = 'test1' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[2] = 'test2' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :text =\> a.to\_json &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Client side: When the AJAX call finishes it calls the following function:

ajax\_onSucces: function(receiveReq) { &nbsp;&nbsp;&nbsp;&nbsp;// convert the json object to an js-array (I'll probably go wrong here) &nbsp;&nbsp;&nbsp;&nbsp;var response = eval("(" + receiveReq.responseText + ")"); &nbsp;&nbsp;&nbsp;&nbsp;alert("resp=" + response) ; &nbsp;&nbsp;&nbsp;&nbsp;for( var i in response ) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(" val=" + i ) ; &nbsp;&nbsp;&nbsp;&nbsp;} },

I see the following text in the alert-boxes:

resp=[object Object],test1,test2 val=0 val=1 val=2 val=each val=eachSlice val=all etc etc

So, what I expected to see was

resp=[object Object],test1,test2 val=[object Object] val=test1 val=test2

Any suggestion where I go wrong ? (or maybe some links to learn more about this subject)

Thnx a lot LuCa
