Home > Web Front-end > JS Tutorial > 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中世界最好的语言
Release: 2018-04-03 10:00:25
Original
2308 people have browsed it

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);
}
});
}
Copy after login

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);
}
}
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template