rails, ajax, json, and script

Could someone please demonstrate the code to send both json (or xml) AND
javascript (to be executed) in a single Ajax request?

Is this even possible?

Could someone please demonstrate the code to send both json (or xml) AND
javascript (to be executed) in a single Ajax request?

Assuming you mean sending that as the response to an ajax request ....

If you set the X-JSON header to be a json fragment Prototype (don't
know about other libraries) will deserialize that for you, and then
the response body can be whatever you want.

Personally if I've already written stuff to take a JSON response and
do stuff based on that data, I wouldn't want to also send back
javascript fragments - I'd just write the client side code to do the
desired action if the appropriate content was in the response JSON

Fred

Frederick Cheung wrote:

I'd just write render :json => {:abc => 1, :xyz => 2}.if you want to
do it via a header because something else is in the body then call
to_json yourself to get the json data and stick it in a header.

Fred

Frederick Cheung wrote:

Setting headers is pretty easy:

response.headers['X-JSON'] = blah.to_json

And then in your client side code look at the the header and read the
JSON (prototype will do this for you, jquery might not)

Fred

Frederick Cheung wrote:

Start with: http://www.w3.org/Protocols/rfc2616/rfc2616.html

I think that may be a bit heavy for starters. Possibly
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol is a bit more
readable.

Colin