I have seen several post about different way to get around special characters not transmitting correctly. But, I was wanting a better and simpler solution (may not find it).
I have a function that I pass an object too. In the object (myObject), there can be some text with special numbered characters (Bill & #8217;s, the right single quotation mark special numbered character).
When I try to send the data, it chokes. So, then I went through my code and escaped everywhere it accepted user input and unescaped where the information was displayed.
Problem:
- Possibly missing places that display or accept user input.
- The data is store with the escaped characters.
I have seen solution where the special characters would be sanitized, but it would require a list of the special characters. Or I could try to decode the escaped characters on the back end.
- Backend - MVC .net C#
jQuery Version: 1.7.2
var json = $.toJSON(myObject); $.ajax({ type: 'POST', url: RootUrl + "Viewer/Save/", data: { "json": json }, async: false, contentType: "application/json; charset=utf-8", success: function (data) { var $("#display").(data); }, error: function (xhr, status, error) { alert(error); } });
More information:
The json string is passed to a MVC controller. The method looks like this.
public ContentResult Save(string json)
{
try
{
dynamic data = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<dynamic>(json);
}
catch (Excepteion ex)
{
// More code
}
}
The error message I get when I am in the Chrome debugger is:
- 500 (Internal Server Error)
More I think about it, I am wondering if there is a setting in the backend that needs to change to accept the special numbered character. The reason I say this is because if I set a break point a the beginning of the MVC controller method, it actually never makes it to the break point. But if escaped or the apostrophe sign is used, it is fine.
I will continue to search and respond back with my findings.
UPDATE
Following the information: Getting "A potentially dangerous Request.Path value was detected from the client (&)"
[ValidateInput(false)]
[HttpPost]
public ContentResult SaveTailoring(string json)
I added this to my code and it solved my problem of getting the data into the controller. I am goig to research so more and would appreciate any thoughts on the pros and cons of this technique.