Front-to-back conversion of C # and JavaScript special characters

Source: Internet
Author: User

Some time ago to do a project, you need to deal with the input of the special characters of the problem. The so-called special characters, mainly refers to in addition to letters, numbers, Chinese characters and other other symbols, the requirements of the project is no matter what the user input characters, can be displayed on the interface as is. We know that JavaScript has symbolic encoding and decoding methods: Escape and Unescape, while C # In fact there are corresponding methods Microsoft.JScript.GlobalObject.escape and Microsoft.JScript.GlobalObject.unescape. How do you use it specifically?

The 1.ajax method takes data from the background to the front end into JSON format, and C # code encodes the special string, otherwise the front end may not convert to JSON-formatted data.

For example, if the string contains double quotes, commas, colons, and other symbols that conflict with JSON format, the JSON conversion will be an error if not converted. At this point the C # code needs to call the Microsoft.JScript.GlobalObject.escape (String str) method to encode the string and organize it into a JSON string response to the front end. Front-End Ajax receives this string when it needs to call JavaScript's unescape (str) method to decode and then use. Of course, you first need to add microsoft.jscript references.

Cases:

Defining strings

String str = "...,:";

StringBuilder builder = new StringBuilder ();

Do not convert output

Builder. Append ("{");
Builder. AppendFormat ("\" content\ ": \" {0}\ "", str);
Builder. Append ("}");

Context. Response.Write (Builder. ToString ());

Ajax-Side code:

$.ajax ({

URL: "Hander.ashx",

DataType: "JSON",
Type: "Get",
Data: {method: "GetName"},
Beforesend:function () {
},
Success:function (data, textstatus) {

if (data) {

The front end uses the data required by the UNESACPE transformation.

var name=unescape (data.name);

//......

}

},

Error:function (XMLHttpRequest, Textstatus, Errorthrown) {

}

C # side code does not convert when the Ajax error method executes, you can get the error message.

and C # code conversion is actually very simple, just need to put the above builder. AppendFormat ("\" content\ ": \" {0}\ "", str) replaced with builder. AppendFormat ("\" content\ ": \" {0}\ "", Microsoft.JScript.GlobalObject.escape (str)).

The 2.ajax method passes data from the front end to the backend. Special characters need to be escape encoded, otherwise when the data is passed by a GET method, an exception occurs if there is a., &, = character, because these characters are the parameter Connector for the URL of the Get method.

var name="?%&=";

$.ajax ({

URL: "Hander.ashx",

DataType: "JSON",
Type: "Get",
Data: {method: "SetName", Name:name},
Beforesend:function () {
},
Success:function (data, textstatus) {

}

The name is then passed to the background, Name=escape (name) for escape encoding.

C # side is very simple to receive, call Microsoft.JScript.GlobalObject.unescape (context. Request[name]).

Said so much, is actually a word of the matter, JavaScript unescape and escape corresponding to C # Methods of Microsoft.JScript.GlobalObject.unescape and Microsoft.JScript.GlobalObject.escape. Just call yourself as needed.

Front-to-back conversion of C # and JavaScript special characters

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.