# Rails removes newlines and spaces from ajax requests

**URL:** <https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954>\
**Category:** Rails\
**Created:** [June 10, 2010, 2:38pm UTC](https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954 "2010-06-10T14:38:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [June 10, 2010, 2:38pm UTC](https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954/1 "2010-06-10T14:38:23Z")

</div>

Hi

I have an app which posts an ajax request with with text area content,  
but when it does post it it gets all the newlines and multiple spaces  
removed (need to use multiple spacing for use with markdown code  
snippets)

I’ve created a sample app which you can see here  
[http://test.42horizons.com/test](http://test.42horizons.com/test),

View it in IE 8 and both requests remove line spaces and new lines,  
view it in firefox 3.6.3 windows and the left request works ok and the  
right request removes line spaces and new lines

the echo action which the requests are sent to simply removes the  
controller and action keys from params and then does

render :text =\> params.to\_json

Anyone have any ideas what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [June 10, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954/2 "2010-06-10T16:14:23Z")

</div>

After look deeper it looks like ActiveSupport::JSON.decode is to  
blame, heres an example

ActiveSupport::JSON.decode(“data=”\u000a function(){  
\u000a var a = 1;\u000a }\u000a\u000a “”)  
=\> data=" function(){ var a = 1; }\n"

Anyone know if theres a fix to this?

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [June 10, 2010, 5:24pm UTC](https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954/3 "2010-06-10T17:24:04Z")

</div>

I’ve change the ajax posting from “data=some data” to “{data:‘some  
data’}”

Hopefully thats what you meant

Unfortunatley this hasnt changed the response, rails is still removing  
the spaces and new lines

Is this what you mean?

On Jun 10, 4:06 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [June 10, 2010, 5:43pm UTC](https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954/4 "2010-06-10T17:43:59Z")

</div>

On Jun 10, 4:15 pm, teesea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve change the ajax posting from “data=some data” to “{data:‘some  
> data’}”

So if you do that then you will need to set the content-type to  
application/json, or else rails will try to parse it as something else

Fred

> Hopefully thats what you meant
> 
> Unfortunatley this hasnt changed the response, rails is still removing  
> the spaces and new lines
> 
> Is this what you mean?

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [June 10, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/rails-removes-newlines-and-spaces-from-ajax-requests/189954/5 "2010-06-10T17:17:37Z")

</div>

On Jun 10, 1:37 pm, teesea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> the echo action which the requests are sent to simply removes the  
> controller and action keys from params and then does
> 
> render :text =\> params.to\_json
> 
> Anyone have any ideas what I’m doing wrong?

I think the way you’re posting data is weird. You’re taking a string,  
encoding it as json but then submitting it as a normal url encoded  
post

Fred
