This article describes how to solve ajax request Garbled text (Chinese Garbled text). For more information, see
Today, I encountered a problem with garbled characters concerning the transmission of Chinese Characters in ajax requests.
The following code:
The Code is as follows:
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 (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:
The Code is as follows:
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 conversion, you can obtain the transmitted Chinese characters.