Home  >  Article  >  Web Front-end  >  How to solve the problem of Chinese garbled characters being transmitted during Ajax requests

How to solve the problem of Chinese garbled characters being transmitted during Ajax requests

php中世界最好的语言
php中世界最好的语言Original
2018-04-03 10:00:252182browse

This time I will show you how to solve the problem of transmitting Chinese garbled characters when Ajax requests. What are the precautions to solve the problem of transmitting Chinese garbled characters when Ajax requests. The following is a practical case, let's take a look.

I encountered a problem today, regarding the problem of garbled characters when transmitting Chinese in an 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 only passes "&foderName="+folderName, the Chinese characters will be garbled. If it is converted twice through encodeURL, the Chinese character encoding will become similar to

The format of "%e6%b5%8b%eb%af%95". After converting to this format, transcode when obtaining, 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to implement AJAX request array

How to clear the cache in Ajax

The above is the detailed content of How to solve the problem of Chinese garbled characters being transmitted during Ajax requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn