Webpages Framework uses @razor syntax to pass JSON strings to JS code

Source: Internet
Author: User

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.

    1. Using the vs2015 ASP. NET Razor3 site template as the basis for gradual expansion, encountered the server passed back to JS JSON string error.
    2. Error behavior: The escape character in the original string \ ", passed to the JS code by the @ variable name, became a "
    3. In the online search, encountered this problem is not much, it should be a primary error;
    4. 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
    5. WORKAROUND: Use HTML for the ' @ variable name ' in the JS code. Row (variable name) is output directly.
    6. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.