I have taken several notes about the error of the returned data after the ajax request with special characters! If the returned content contains single quotation marks and double quotation marks, etc! They will intercept the string and report an error because the json format is incorrect! I have been using the following method: name. Replace ("'", "% 27"). Replace ("\" "," % 22 ")! Today I will be more like this!
Core code: string json = "{Id: 1, Name: \" "+ Microsoft. JScript. globalObject. escape (name) + "\", Sex: \ "male \"}";
Microsoft. JScript. GlobalObject: there are many methods in the class that correspond to the front-end js! For example: decodeURI, encodeURIComponent etc! If you are interested, check it out.
1. First, the project references the Microsoft. JScript class library!
2. Background use: Microsoft. JScript. GlobalObject. escape (name)
3. Foreground use: unescape (s. Name );
Page:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <body> <form id = "form1" runat = "server"> <asp: scriptManager ID = "ScriptManager1" runat = "server"> </asp: ScriptManager> <script type = "text/javascript"> function GetJson () {var xmlHttp; try {// Firefox, Opera 8.0 +, Safari xmlHttp = new XMLHttpRequest ();} catch (e) {// Internet Explo Rer try {xmlHttp = new ActiveXObject ("Msxml2.XMLHTTP");} catch (e) {try {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");} catch (e) {alert (" your browser does not support AJAX! "); Return false ;}} xmlHttp. onreadystatechange = function () {if (xmlHttp. readyState = 4) {// alert (xmlHttp. responseText); var str = xmlHttp. responseText; alert (str); // $ get ("divShow "). innerHTML = xmlHttp. responseText; // eval ('var s = {"id": 1} '); // eval ("var s =" + xmlHttp. responseText + ")"; // s = {"id": 1}; try {eval ("var s =" + str); alert ("sex: "+ unescape (s. sex) + "\ r \ nname:" + unescape (S. name);} catch (e) {alert (e. message) }}var data = "id = 1"; xmlHttp. open ("POST", "default. aspx? Ajax = true ", true); xmlHttp. send (data) ;}</script> <center> <div style = "color: red "> Test <input type =" button "value =" GetJson "onclick =" GetJson (); "/> <div id =" divShow "> </div> </center> </form> </body>
Using Microsoft. sharePoint; using System. diagnostics; using System. data; // note that you need to add reference to the project: Microsoft. JScript class library public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (IsPostBack) return; if (! String. isNullOrEmpty (Request. queryString ["ajax"]) {string name = "hao 'jiang \" fesd "; string sex =" sex 'you \ "lok"; // 1. string json = "{Id: 1, Name: \" "+ Server. urlEncode (name) + "\", Sex: \ "male \"} "; string json =" {Id: 1, Name: \ "" + Microsoft. JScript. globalObject. escape (name) + "\", Sex: \ "" + Microsoft. JScript. globalObject. escape (sex) + "\"} ";/*** sorry, because I have not solved this problem before, I have been using a very difficult solution: string json = "{Id: 1, Name: \ "" + Name. replace ("'", "% 27 "). replace ("\" "," % 22 ") +" \ ", Sex: \" male \ "}"; * this method is used, my heart has been shadow [haha!], I always try my best to get answers from google! At last, Huang Tian was born! I found it! I think this method is better! * **/Response. write (json);/** Note: If the page submitted by ajax is aspx. * Response. end (); otherwise, you will print out the characters you need, and then print the HTML of the page *. I didn't know it before and didn't execute Response. end (), xmlhttprequest. responseText: * it still needs to be truncated! * Remember to call Response. End () every time. If you are interested, comment out Response. End (); and try again! **/Response. End ();}}
}
I have not solved this problem in the following article!
AJAX is submitted to Handler. ashx. The general Handler returns json data.
Technorati labels: ajax, asp.net