JQuery向ashx提交中文参数方案
2014-04-16 11:12
183 查看
客户端:
//异步获取数据
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','');
服务器端
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!
//异步获取数据
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','');
服务器端
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!
相关文章推荐
- JQuery向ashx提交中文参数方案
- JQuery向ashx提交中文参数方案 [转]
- 页面编码为GBK时_jquery_提交ajax数据完美解决中文乱码方案
- 页面编码为GBK时 jquery 提交ajax数据完美解决"中文乱码"方案
- ASP.NET使用Jquery-Ajax向ashx传递参数中文出现乱码
- JQuery中Ajax的Post提交中文乱码、windows.location.href 中文汉字乱码和springmvc使用原生态HttpServletRequest接收参数中文乱码解决办法
- 页面编码为GBK时 jquery 提交ajax数据完美解决"中文乱码"方案
- 页面编码为GBK时 jquery 提交ajax数据完美解决中文乱码方案
- Jquery来对form表单提交(mvc方案)
- get方式地址栏传中文参数乱码 及 form表单利用jquery.serialize()序列化中文参数乱码 解决总结
- jQuery Form Plugin:AJAX方式提交表单的完全方案
- 解决jQuery $.getJSON() 传递参数中文乱码问题
- jQuery幻灯片插件owlcarousel参数说明中文文档
- jquery提交中文导致乱码
- jquery的ajax向ashx传中文,出现乱码的解决方法
- 这才是真正的JQuery.ajax传递中文参数的解决方法
- jquery 获取url 参数方法 以及 解决url中文问题
- SpringMVC系列(三)JQuery和JSON方式参数传递并处理JAVAWEB中文乱码问题
- JavaWeb学习——Ajax提交URL中文参数乱码问题
- 彻底解决get显示提交时,参数中包含中文时出现乱码的问题