转自:http://blog.csdn.net/wangqiuyun/article/details/8450964

字符编码这个东西,一旦和中文打上交道就不可避免出现乱码,今天项目用到了JQuery向ashx提交中文参数的这一块,折腾了一天,开始是各种乱码,最后中算弄好了。

客户端:

  1. //异步获取数据
  2. var tpAction=action+'getSearch.ashx?key='+encodeURIComponent('中国');
  3. $.getJSON(tpAction,function(rsp){//请求成功
  4. if(rsp.status=='1'){
  5. var list=rsp.item;
  6. var len=list.length;
  7. if(len==0){
  8. //没有数据直接返回
  9. setNull();
  10. return;
  11. }
  12. //组装数据
  13. var s=zy_tmpl(templ,list,len);
  14. $list.append(s);
  15. }else{
  16. alert('','加载数据失败,请重试','确定');
  17. }
  18. },'json',function(err){//请求失败
  19. alert('','加载失败,请检查网络设置!','确定');
  20. },'POST','');
//异步获取数据
var tpAction=action+'getSearch.ashx?key='+encodeURIComponent('中国');
$.getJSON(tpAction,function(rsp){//请求成功
if(rsp.status=='1'){
var list=rsp.item;
var len=list.length;
if(len==0){
//没有数据直接返回
setNull();
return;
}
//组装数据
var s=zy_tmpl(templ,list,len);
$list.append(s);
}else{
alert('','加载数据失败,请重试','确定');
} },'json',function(err){//请求失败
alert('','加载失败,请检查网络设置!','确定');
},'POST','');

服务器端:

  1. string keyy = context.Request["key"];
  2. if (keyy != null)
  3. {
  4. string key = HttpUtility.UrlDecode(context.Request["key"].ToString());
  5. //string key = context.Server.HtmlDecode(context.Request.Params["key"].ToString());
  6. string log = "getSearch.ashx?key=" + key;
  7. string logsql = "insert into logs(createtime,contents) values('" + DateTime.Now.ToString() + "','" + log + "')";
  8. Sqlbase.ExecuteNonQuery(CommandType.Text, logsql, null);
  9. //more
  10. string sql = "select * from news where title like '%" + key + "%' order by id desc";
  11. DataTable dt = Sqlbase.ExecuteTable(CommandType.Text, sql, null);
  12. result rs = new result();
  13. rs.status = 1;
  14. rs.msg = "成功";
  15. rs.item = dt;
  16. string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(rs);
  17. context.Response.Clear();
  18. context.Response.ContentEncoding = Encoding.UTF8;
  19. context.Response.ContentType = "application/json";
  20. context.Response.Write(strJson);
  21. context.Response.Flush();
  22. context.Response.End();
  23. }
        string keyy = context.Request["key"];

        if (keyy != null)
{ string key = HttpUtility.UrlDecode(context.Request["key"].ToString());
//string key = context.Server.HtmlDecode(context.Request.Params["key"].ToString());
string log = "getSearch.ashx?key=" + key;
string logsql = "insert into logs(createtime,contents) values('" + DateTime.Now.ToString() + "','" + log + "')";
Sqlbase.ExecuteNonQuery(CommandType.Text, logsql, null); //more
string sql = "select * from news where title like '%" + key + "%' order by id desc";
DataTable dt = Sqlbase.ExecuteTable(CommandType.Text, sql, null);
result rs = new result();
rs.status = 1;
rs.msg = "成功";
rs.item = dt;
string strJson = Newtonsoft.Json.JsonConvert.SerializeObject(rs); context.Response.Clear();
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.ContentType = "application/json";
context.Response.Write(strJson);
context.Response.Flush();
context.Response.End();
}

这里根本就没有进行什么编码设置,其实就是JS用encodeURIComponent()对中文字符串编码,C#用HttpUtility.UrlDecode(context.Request["key"].ToString())进行解码,是不是很简单呢!可是我一天的实践证明:JS端用encodeURIComponent()要比escape()好多了,至于区别大家可以google一下,C#的解码函数也一堆,像什么Server.UrlDecode之类的,但是推荐使用HttpUtility.UrlDecode(),好处大家同样也可以google!

最新文章

  1. .Net中的AOP系列之《间接调用——拦截方法》
  2. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
  3. windows平台,WEB开发,喜欢IE + notepad的同行进来可以看。
  4. CMD执行BCP命令
  5. Linux动态库的搜索路径
  6. Android:Java代码添加按钮
  7. 谈 DevOps 自动化时,也应该考虑到 SOX 等法案
  8. java日历类Calendar简单使用
  9. 【Bower】
  10. 多线程爬虫Java调用wget下载文件,独立线程读取输出缓冲区
  11. JS获取DropDownList的value值与text值
  12. mybatis与mysql插入时返回主键id的值
  13. AutoFac使用方法总结:Part I
  14. 关于jquery 1.9以上多次点击checkbox无法选择的
  15. Linux的网卡由eth0变成了eth1,如何修复
  16. PHP大法
  17. HDU 5115 (杀狼,区间DP)
  18. 【Python爬虫】01:网络爬虫--规则
  19. 【iCore1S 双核心板_ARM】例程十七:FSMC实验——读写FPGA
  20. Delphi自写组件:可设置颜色的按钮

热门文章

  1. 谈谈bootstrap在实践中的应用
  2. .Net创建Windows服务完成批量导出功能(错误速查)
  3. 日常-acm-排列
  4. 学习Rust Book之写Cargo配置文件
  5. linux 硬链接与软链接的区别
  6. 第七章 动态创建HTML内容
  7. python基础一 day16 匿名函数
  8. Django项目中"expected str, bytes or os.PathLike object, not list"错误解决:
  9. Mbps、Kbps、bps、kb、mb区别和换算
  10. vue.js 独立引用css文件图片路径错误