问题描述
通过ashx读取数据库返回json数组绑定select.返回的json内容里有中文值、无法使用、改为只返回值是英文的属性就可以使用勒,怎么解决勒?tips:1、ashx,js,html都已另存为utf-8类型勒;2、ashx和js代码(一部分):StringBuildersb=newStringBuilder("[");stringtpl=""id":{0},"text":{1}";foreach(DataRowdrinds.Tables[0].Rows){sb.Append("{"+string.Format(tpl,dr["provinceid"],dr["provincename"])+"},");}context.Response.ContentEncoding=Encoding.UTF8;context.Response.Write(sb.ToString().Substring(0,sb.ToString().Length-1)+"]");
$(function(){$.getJSON("ajax/getaddress.ashx?type=p",function(data){$.each(data,function(i,v){$("#selProvince").append("<optionvalue='"+v.id+"'>"+v.text+"</option>");});});});
直接运行ashx,页面输出json数组正常:[{"id":1,"text":北京},{"id":2,"text":天津},{"id":3,"text":河北},{"id":4,"text":山西},{"id":5,"text":内蒙古自治区},{"id":6,"text":辽宁},{"id":7,"text":吉林},{"id":8,"text":黑龙江},{"id":9,"text":上海},{"id":10,"text":江苏},{"id":11,"text":浙江},{"id":12,"text":安徽},{"id":13,"text":福建},{"id":14,"text":江西},{"id":15,"text":山东},{"id":16,"text":河南},{"id":17,"text":湖北},{"id":18,"text":湖南},{"id":19,"text":广东},{"id":20,"text":广西壮族自治区},{"id":21,"text":海南},{"id":22,"text":重庆},{"id":23,"text":四川},{"id":24,"text":贵州},{"id":25,"text":云南},{"id":26,"text":西藏自治区},{"id":27,"text":陕西},{"id":28,"text":甘肃},{"id":29,"text":青海},{"id":30,"text":宁夏回族自治区},{"id":31,"text":新疆维吾尔自治区},{"id":32,"text":香港特别行政区},{"id":33,"text":澳门特别行政区},{"id":34,"text":台湾}]
解决方案
解决方案二:
。。。CSDN现在好冷清。
解决方案三:
解决方案四:
参考:
解决方案五:
c#先编码下HttpUtility.UrlEncode();js解码encodeURI()
解决方案六:
引用2楼sandy945的回复:
http://mysun.iteye.com/blog/382053
我ashx里中文值的部分加了编码、然后js这边解码还是不行勒StringBuildersb=newStringBuilder("[");stringtpl=""pid":{0},"pname":{1}";foreach(DataRowdrinds.Tables[0].Rows){sb.Append("{"+string.Format(tpl,dr["provinceid"],HttpUtility.UrlEncode(dr["provincename"].ToString()))+"},");}context.Response.Write(sb.ToString().Substring(0,sb.ToString().Length-1)+"]");
$.getJSON("ajax/getaddress.ashx?type=p",function(data){$.each(data,function(i,v){$("#selProvince").append("<optionvalue='"+v.pid+"'>"+decodeURI(v.pname)+"</option>");});});
ashx输出正常:[{"pid":1,"pname":%e5%8c%97%e4%ba%ac},{"pid":2,"pname":%e5%a4%a9%e6%b4%a5},{"pid":3,"pname":%e6%b2%b3%e5%8c%97},{"pid":4,"pname":%e5%b1%b1%e8%a5%bf},{"pid":5,"pname":%e5%86%85%e8%92%99%e5%8f%a4%e8%87%aa%e6%b2%bb%e5%8c%ba},{"pid":6,"pname":%e8%be%bd%e5%ae%81},{"pid":7,"pname":%e5%90%89%e6%9e%97},{"pid":8,"pname":%e9%bb%91%e9%be%99%e6%b1%9f},{"pid":9,"pname":%e4%b8%8a%e6%b5%b7},{"pid":10,"pname":%e6%b1%9f%e8%8b%8f},{"pid":11,"pname":%e6%b5%99%e6%b1%9f},{"pid":12,"pname":%e5%ae%89%e5%be%bd},{"pid":13,"pname":%e7%a6%8f%e5%bb%ba},{"pid":14,"pname":%e6%b1%9f%e8%a5%bf},{"pid":15,"pname":%e5%b1%b1%e4%b8%9c},{"pid":16,"pname":%e6%b2%b3%e5%8d%97},{"pid":17,"pname":%e6%b9%96%e5%8c%97},{"pid":18,"pname":%e6%b9%96%e5%8d%97},{"pid":19,"pname":%e5%b9%bf%e4%b8%9c},{"pid":20,"pname":%e5%b9%bf%e8%a5%bf%e5%a3%ae%e6%97%8f%e8%87%aa%e6%b2%bb%e5%8c%ba},{"pid":21,"pname":%e6%b5%b7%e5%8d%97},{"pid":22,"pname":%e9%87%8d%e5%ba%86},{"pid":23,"pname":%e5%9b%9b%e5%b7%9d},{"pid":24,"pname":%e8%b4%b5%e5%b7%9e},{"pid":25,"pname":%e4%ba%91%e5%8d%97},{"pid":26,"pname":%e8%a5%bf%e8%97%8f%e8%87%aa%e6%b2%bb%e5%8c%ba},{"pid":27,"pname":%e9%99%95%e8%a5%bf},{"pid":28,"pname":%e7%94%98%e8%82%83},{"pid":29,"pname":%e9%9d%92%e6%b5%b7},{"pid":30,"pname":%e5%ae%81%e5%a4%8f%e5%9b%9e%e6%97%8f%e8%87%aa%e6%b2%bb%e5%8c%ba},{"pid":31,"pname":%e6%96%b0%e7%96%86%e7%bb%b4%e5%90%be%e5%b0%94%e8%87%aa%e6%b2%bb%e5%8c%ba},{"pid":32,"pname":%e9%a6%99%e6%b8%af%e7%89%b9%e5%88%ab%e8%a1%8c%e6%94%bf%e5%8c%ba},{"pid":33,"pname":%e6%be%b3%e9%97%a8%e7%89%b9%e5%88%ab%e8%a1%8c%e6%94%bf%e5%8c%ba},{"pid":34,"pname":%e5%8f%b0%e6%b9%be}]
解决方案七:
引用4楼zhouyongli41的回复:
c#先编码下HttpUtility.UrlEncode();js解码encodeURI()
解码是decodeURI吧,我试了,还是不行,难道人品问题?
解决方案八:
LZ,你这杯具啊……字符串啊,引号都不加。{"id":1,"text":"北京"}
解决方案九:
。。。确实