ajax请求乱码的解决方法(中文乱码)_javascript技巧
程序员文章站
2022-04-13 11:54:54
...
今天遇到一个问题,有关ajax请求中传输中文,遇到乱码的问题。
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);
}
});
}
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);
}
}
}
如下代码:
复制代码 代码如下:
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);
}
});
}
如上代码 如果只是传“&foderName=”+folderName 的话,汉字就会产生乱码,如果经过encodeURL 转换两次的话,汉字编码会变为类似
“%e6%b5%8b%eb%af%95” 的格式。转换为这种格式之后,在获取的时候在转码,如下所示:
复制代码 代码如下:
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);
}
}
}
经此转换之后,即可获取到传输的汉字。
推荐阅读
-
解决3.01版的jquery.form.js中文乱码问题的解决方法_jquery
-
php读取mysql中文数据出现乱码的解决方法_PHP教程
-
php substr截断中文半个汉字乱码问题的解决方法
-
动态加载iframe时get请求传递中文参数乱码解决方法_javascript技巧
-
PHPMailer邮件标题中文乱码的解决方法
-
ajax的配置详情、ajax的调用解释、ajax的中文乱码和ajax的表单提交(内有实例)
-
phpmailer 发送邮件中文乱码问题的解决方法总结
-
jquery的ajax()函数传值中文乱码解决方法介绍
-
IE6通过get发送奇数个汉字请求会乱码的解决方法_PHP教程
-
ajax 乱码的一种解决方法