AJAX: send JSONified ruby-array back to client problem

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs