Solution to the Problem of Chinese garbled characters transmitted in Ajax requests
Today, I encountered a problem with garbled characters concerning the transmission of Chinese Characters in ajax requests.
The following code:
function UpdateFolderInfoByCustId(folderId, folderName, custId) {$.ajax({type: "Post",contentType: "application/x-www-form-urlencoded; charset=utf-8",url: "http://localhost/CRM/Ashx/HandKBSucessCustomer.ashx?Method=UpdateCustomerByCustId&folderId=" + folderId + "&folderName=" + encodeURI(encodeURI(folderName)) + "&custId=" + custId,success: function (msg) {alert(msg);},error: function (error) {alert(error);}});}
If the above Code only transmits "& foderName =" + folderName, Chinese characters will produce garbled characters. If it is converted twice through encodeURL, the Chinese character encoding will become similar
"% E6 % b5 % 8b % eb % af % 95" format. After being converted to this format, transcoding is performed when obtaining it, as shown below:
public void UpdateCustomerByCustId(){int folderId = Convert.ToInt32(Request["folderId"]);string folderName = Request["folderName"];string folderName2 = Convert.ToString(System.Web.HttpUtility.UrlDecode(folderName));int custId = Convert.ToInt32(Request["custId"]);bool res = false;try{res = CustomerBusiness.UpdateCustomerByCustId(folderId, folderName2, custId);}catch (Exception ex){throw;}Response.Write(res);}}}
After this conversion, you can obtain the transmitted Chinese characters.
The above is a small series of solutions to the problem of transmitting Chinese garbled characters in Ajax requests. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. I would like to thank you for your support for the help House website.