# Error when trying to send JSON from rails to javascript/jQuery

**URL:** <https://www.ruby-forum.com/t/error-when-trying-to-send-json-from-rails-to-javascript-jquery/192475>\
**Category:** Rails\
**Created:** [July 30, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/error-when-trying-to-send-json-from-rails-to-javascript-jquery/192475 "2010-07-30T20:50:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lucas](https://www.ruby-forum.com/u/lucas)\
**Post date:** [July 30, 2010, 8:50pm UTC](https://www.ruby-forum.com/t/error-when-trying-to-send-json-from-rails-to-javascript-jquery/192475/1 "2010-07-30T20:50:51Z")

</div>

Hello,

I’m trying to send the code for a google ad from my server to a web  
browser. The code is not embedded in the original HTML of the webpage.  
The code is sent to the browser while page is rendering. We are  
sending the google code on-the-fly like this because we have our own  
ad targeting.

We have a controller method on our server (“foo”) that sends this  
reply to the browser:

```
render :text => "var response = { content: '#{ad_code}', id:

```

‘#{div\_where\_the\_ad\_will\_appear}’ };"

This method is called by our javascript code on the client-side:

```
$.getScript( url_of_foo_method, callback_function );

```

The $.getScript function allows us to perform an ajax call (using  
JSONP, not regular JSON) to our server.

When the adcode is just one line and is regular HTML, like this:  
ad\_code = “\<a href=“http://adsrv1.com/a/?902/51220/CD8665/225400/  
&optionalinfo=”\>\<img src=“http://adsrv2.com/ai/?  
902/51220/8665/225400” alt=”" border=“0”\>"

…then everything works fine.

But when the ad\_code has google adsense code in it, which uses script  
tags and is multiple lines, like this:

```
ad_code = "<script type="text/javascript"><!--
ad_stuff1 = "value1";
ad_stuff2 = "value2";
//--><\/script>
<script type="text/javascript"
  src="http://adstuff/js_file.js">
<\/script>"

```

…we get this error in the Firebug debugger:

```
unterminated string literal
[Break on this error] var response = { content: '<script

```

type=“text/javascript”\>\</script\>  
  
\</script\>  
', id: ‘id1’ };

The google ad\_code is not all on one line, but as you can see in the  
error, is multiple lines. So there is an unterminated string literal,  
perhaps inside the google\_ad\_code we put there. I’m trying to  
understand if the quotes and newlines in the google ad\_code are  
somehow causing a problem. Perhaps the quotes are being escaped wrong  
because this is inside javascript and we’re using JSONP?

I’ve spent all day trying to fix this, to no avail.  
Maybe only the first line “var response = { content: '\<!–”  
is processed by javascript. Maybe the end of line character breaks the  
string. I don’t know.

In the controller, I’ve added these 2 lines before the render :  
code = code.gsub(/\n/,"\n")  
code = code.gsub(//script/,"\/script")  
So as to avoid the possible end of line string breaking. I gsubbed the  
script tag for the same reasons.

But these fixes do nothing.  
Does anyone have any ideas or advice ?

Thanks
