Question

I'm getting encoded data from the server, which is encoded using .NETs WebUtility.HtmlEncode.

This data is then displayed and needs to be sent back to the server for some operations. During this time, it is converted to JSON before being sent over using JSON.stringify. All works fine so far.

However, once this reaches the server, it is rejected due to being potentially dangerous. The object that is converted to JSON can have strings with special chars such as -

"This is John&#39s account" originally "This is John's account"

Or "John earns in &#165" originally "John earns in ¥"

My belief is that these encoded string values are interfering with the JSON being properly formed.

Is there any way in Javascript that I can JSONify HTML encoded strings?

EDIT: In case it's not clear, the data is already encoded when i do JSON.stringify(data). An example of my data -

row[0] = {column1, column2, column3} Where each column is an HTML encoded string such as "This is John&#39s account"

Was it helpful?

Solution 3

The solution in the end, was more of a hack, I added an annotation -

[ValidateInput(false)]

to my function on the back-end, so that it wouldn't try to validate my JSON string.

OTHER TIPS

Considering that a JSON object with a string would look like this

{ 'member1' : 'some string with &#165' }

I don't believe it's the JSON at fault. It is far more likely that you are passing the JSON object to a method via GET instead of POST.

As a particular example, the Microsoft MVC3 framework will throw an error about it being unsafe if you submit JSON via a GET method and don't specify to allow GET behavior.

The reason for this can be seen in this answer.

I think you can achieve this functionality in three steps:

  1. Create a partial view.
  2. Call this partial view by passing your string values in it and perform action there.
  3. Return your partial view via JSON and replace it with old one.

But returning the partial view via JSON is bit tricky, I mean you cannot just return the partial view via JSON. First you need to convert the partial view in string and the return this string. Below method will you how to achieve this:

    public string RenderRazorViewToString(string viewName, object model)
    {
        ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }

This method will convert the partial view in string and return it back to server via JSON. You need to pass to parameter in it, first is the partial view name and second is model. Hope you will get solution of your problem by this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top