Encoding NewLines & Quotes in jSON

by Randall 7/20/2010 11:04:00 AM

Today I attempted to send a giant amount of user-entered text from a standard HTML page, via jQuery's .ajax() method - passing a jSON packet to a REST-enabled WCF service.

It was failing.

Turns out the newlines were destroying the jSON formatting, so I needed to find a way to preserve the information for the server.  I ran across a blog post dealing with this here,  and he suggested the following bit of code:

function escapeNewLineChars(valueToEscape) {
    if (valueToEscape != null && valueToEscape != "") {
        return valueToEscape.replace(/\n/g, "\\n");
    } else {
        return valueToEscape;
    }
}

It didn't work.

Turns out, after fiddling with it, the "replace with" value needed to be wrapped in single (not double) quotes. So, in order to prepare your text for transmittal via jSON, try this:

function fixMyUserEnteredData(giantBlobOfText) {

giantBlobOfText = $.trim(giantBlobOfText.replace(/\n/g, '\\n')); // converts the newlines and trims the string

return giantBlobOfText.replace(/\"/g, '\\"')); // converts all double quotes

}

That did the trick.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Fixes

This Was Probably Sheer Luck ...

by Randall 7/31/2008 1:11:00 PM

I just spent part of two days battling an exception while waiting on a response from a web service. 

    ---> System.Net.WebException: The server committed a protocol violation. Section=ResponseStatusLine at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest     request)

   at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request)

   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) 

I found all sorts of suggestions to fix the issue.  Most of them involved adding the following information to your web.config:

    <system.net>
        <settings>
            <httpWebRequest useUnsafeHeaderParsing="true" />
        </settings>
    </system.net> 

No luck.

I even found a post that suggested you override the WebRequest method within your proxy class (seems like a bad idea because if you ever regenerate your proxy class ... well, you know what'll happen to your "fix").

protected override WebRequest GetWebRequest(Uri uri)
{
     HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(uri);

     request.ProtocolVersion = HttpVersion.Version10;

     return request;
} 

Anyway, after neither of those solutions had worked, my Google resources were running low.  I kept noticing that all of the posts mentioned .Net 1.1 ...

So I changed the webservice to use 2.0 (this is a COTS product and installs to 1.1 by default) and voila.  No more nasty gram.

Perhaps this will help someone.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Fixes

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Randall Sexton
Currently a .Net developer for Bechtel Corporation in Oak Ridge, TN.

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Pages

    Recent comments

    Don't show

    Authors

    Categories


    Logo Credit

    My logo was taken from CodingHorror.
    Jeff Atwood © Copyright 2007

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Sign in