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?

On Jun 8, 3:00 pm, Ralph S. [email protected] wrote:

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 C. wrote:

On Jun 8, 3:00�pm, Ralph S. [email protected] wrote:

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.

Fred:

Ok … lets say I have two variables ABC and XYZ and I want to send the
values of those two variables back to my jQuery code …

What’s the magic invocation to do that in Rails?

On Jun 8, 3:56 pm, Ralph S. [email protected] wrote:

Frederick C. wrote:

On Jun 8, 3:00 pm, Ralph S. [email protected] wrote:

Fred:

Ok … lets say I have two variables ABC and XYZ and I want to send the
values of those two variables back to my jQuery code …

What’s the magic invocation to do that in Rails?

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 C. wrote:

On Jun 8, 3:56�pm, Ralph S. [email protected] wrote:

Frederick C. wrote:

On Jun 8, 3:00 pm, Ralph S. [email protected] wrote:

Fred:

Ok … lets say I have two variables ABC and XYZ and I want to send the
values of those two variables back to my jQuery code …

What’s the magic invocation to do that in Rails?

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

Fred (or anyone):

Do you know of any tutorial that explains this header stuff?

Frederick C. wrote:

On Jun 8, 4:51�pm, Ralph S. [email protected] wrote:

Do you know of any tutorial that explains this header stuff?

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

Fred and all:

Thank you, Fred.

What is all this header and body stuff? I’d really like to understand
this.

Is there an overview? I have Googled stuff but … I’m not finding any
meat.

On Tue, Jun 8, 2010 at 12:51 PM, Ralph S. [email protected]
wrote:

What is all this header and body stuff? I’d really like to understand
this.

Start with: Hypertext Transfer Protocol -- HTTP/1.1


Hassan S. ------------------------ [email protected]
twitter: @hassan

On 8 June 2010 21:06, Hassan S. [email protected]
wrote:

On Tue, Jun 8, 2010 at 12:51 PM, Ralph S. [email protected] wrote:

What is all this header and body stuff? I’d really like to understand
this.

Start with: Hypertext Transfer Protocol -- HTTP/1.1

I think that may be a bit heavy for starters. Possibly
HTTP - Wikipedia is a bit more
readable.

Colin

On Jun 8, 4:51 pm, Ralph S. [email protected] wrote:

Do you know of any tutorial that explains this header stuff?

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