# Rails, ajax, json, and script

**URL:** <https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834>\
**Category:** Rails\
**Created:** [June 8, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834 "2010-06-08T16:00:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 4:00pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/1 "2010-06-08T16:00:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/2 "2010-06-08T16:14:54Z")

</div>

On Jun 8, 3:00 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/3 "2010-06-08T16:56:20Z")

</div>

Frederick C. wrote:

> On Jun 8, 3:00ï¿½pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 5:16pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/4 "2010-06-08T17:16:23Z")

</div>

On Jun 8, 3:56 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Frederick C. wrote:
> 
> > On Jun 8, 3:00 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/5 "2010-06-08T17:51:18Z")

</div>

Frederick C. wrote:

> On Jun 8, 3:56ï¿½pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Frederick C. wrote:
> > 
> > > On Jun 8, 3:00 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 9:51pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/6 "2010-06-08T21:51:31Z")

</div>

Frederick C. wrote:

> On Jun 8, 4:51ï¿½pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 10:08pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/7 "2010-06-08T22:08:33Z")

</div>

On Tue, Jun 8, 2010 at 12:51 PM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

Start with: [Hypertext Transfer Protocol -- HTTP/1.1](http://www.w3.org/Protocols/rfc2616/rfc2616.html)

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 10:15pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/8 "2010-06-08T22:15:35Z")

</div>

On 8 June 2010 21:06, Hassan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On Tue, Jun 8, 2010 at 12:51 PM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > What is all this header and body stuff? I’d really like to understand  
> > this.
> 
> Start with: [Hypertext Transfer Protocol -- HTTP/1.1](http://www.w3.org/Protocols/rfc2616/rfc2616.html)

I think that may be a bit heavy for starters. Possibly  
[HTTP - Wikipedia](http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is a bit more  
readable.

Colin

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [June 8, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/rails-ajax-json-and-script/189834/9 "2010-06-08T18:02:38Z")

</div>

On Jun 8, 4:51 pm, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
