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 () + "& 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 =
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.