当前位置:编程学习 > C#/ASP.NET >>

ASP.NET中AJAX乱码解决方法

ajax乱码是一个程序员经常会碰到的问题,今天我们要讲的是关于asp教程.net中ajax乱码解决方法哦,下面先详细的介绍了处理方法,然后再举例说明

利用asp.net教程

string s = system.text.encoding.utf8.getstring(request.binaryread(request.contentlength));
  system.collections.specialized.namevaluecollection form = httputility.parsequerystring(s);

  text_supplier.text = form[text_supplier.uniqueid];
  // order.remark = text_supplier.text;


利用 escape

<%
       liststr="ajax中文乱码的简单解决方法"
       sponse.write escape(liststr) '用escape编码
%>

           2、客户端网页特效程序
function toserver(url)

{
var req = new xmlhttprequest();
if (req) {
   req.onreadystatechange = function() {
    if (req.readystate == 4 )
    {
     if(req.status == 200 || req.status == 304) {
       getstr=unescape(req.responsetext) '用unescape解码
             alert(getstr);
}
                    else
     {return false;}
    }
   }
         req.open('get', url);
   req.setrequestheader('content-type', 'application/x-www-form-urlencoded; charset=utf-8');
   req.send(null);
}

}


总结上面的实例,现在来看一个详细的asp.net ajax乱码处理方法

var url=service.aspx?value=' + escape(va);
            obj=createajax();
            if(obj)
            {
              obj.open('get',url,true);
              obj.send(null);
            }

asp.net的escape函数

        private string escape(string s)
        {
            stringbuilder sb = new stringbuilder();
            byte[] ba = system.text.encoding.unicode.getbytes(s);
            for (int i = 0; i < ba.length; i += 2)
            {    /**///// be sure 2's
                sb.append("%u");
                sb.append(ba[i + 1].tostring("x2"));
                sb.append(ba[i].tostring("x2"));
            }
            return sb.tostring();

        }

asp.net处理页面

response.contenttype = "text/html";
response.charset = "gb2312";
response.clear();
response.write(escape(返回的字符串));


注:

ajax默认发送的编码是uft-8的模式。

补充:asp.net教程,C语言
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,