The time to go to web development is too short, one tries to do several junior projects and encounters too many difficulties. Although not to learn to develop a professional, just for hobbies so the bite-bullet insisted.
Keep a record of the problems encountered.
- Using the vs2015 ASP. NET Razor3 site template as the basis for gradual expansion, encountered the server passed back to JS JSON string error.
- Error behavior: The escape character in the original string \ ", passed to the JS code by the @ variable name, became a "
- In the online search, encountered this problem is not much, it should be a primary error;
- Read an article for the following reasons: in order to prevent certain attacks, the system processed the sensitive characters, and then there was the \ "= " The result of the original link
- WORKAROUND: Use HTML for the ' @ variable name ' in the JS code. Row (variable name) is output directly.
- The problem is resolved.
For example:
The server-side code is as follows:
System.Web.Script.Serialization.JavaScriptSerializer JSS = new System.Web.Script.Serialization.JavaScriptSerializer ();
var jsonstr = JSS. Serialize (Temjson);
At this point: using breakpoints, monitor jsonstr, the results are correct, and some strings are as follows:
"[{\" id\ ": 905,\" production date \ ": \" \\/date (1460649600000) \\/\ ", \" month \ ": 4,。。。。。。 }]"
The JS reference jsonstr code is:
var json=json.parse (' @jsonStr ');
Try to run, ie to the error here, the string in the \ " into a "
Modify the line code to:
var json=json.parse (' @ (Html.raw (jsonstr)) ');
Run again and the results are correct.
Webpages Framework uses @razor syntax to pass JSON strings to JS code