Ajax request garbled code solution of Chinese garbled code

  • 2020-03-30 02:37:00
  • OfStack

Today, I encountered a problem related to the transfer of Chinese in the ajax request.

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 is just passed "&foderName=" +folderName, the Chinese character will generate chaotic code, if the encodeURL is converted twice, the Chinese character code will become similar

"%e6%b5%8b%eb%af%95" format. After converting to this format, transcoding is performed at the time of acquisition, 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, the transmitted Chinese characters can be obtained.


Related articles: