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

**URL:** <https://www.ruby-forum.com/t/ajax-send-jsonified-ruby-array-back-to-client-problem/124818>\
**Category:** Rails\
**Created:** [January 2, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/ajax-send-jsonified-ruby-array-back-to-client-problem/124818 "2008-01-02T21:15:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [January 2, 2008, 9:15pm UTC](https://www.ruby-forum.com/t/ajax-send-jsonified-ruby-array-back-to-client-problem/124818/1 "2008-01-02T21: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|  
…  
format.js { # AJAX call  
a = [{}] # an array with an hash  
a[0][‘xyz’] = ‘abc’  
a[1] = ‘test1’  
a[2] = ‘test2’  
render :text =\> a.to\_json  
}  
end

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

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

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
