免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4266 | 回复: 9
打印 上一主题 下一主题

[编码] prototype编码问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-12 08:53 |只看该作者 |倒序浏览
看了limodou的教程,我在网上找了一份prototype.js试了一下,发现它有个Ajax.Updater用来更新html的挺好用,但是有点小问题, Ajax.Updater从服务器端返回页面的编码是gb2312,但是主页面更新了之后却成了乱码,不知道哪位能帮助解决这个问题
ps:
我的服务器用的是mod_python,用了2个文件test.py和test.psp

test.py

  1. import pgdb
  2. from mod_python import psp,apache,util

  3. def index(req):
  4.     req.content_type='text/html'
  5.     conn=pgdb.connect(host='localhost',database='test,user='test',password='test')
  6.     cur=conn.cursor()
  7.     sql="select * from schstu"
  8.     cur.execute(sql)
  9.     allfields=cur.description
  10.     allrecords=cur.fetchall()
  11.     cur.close()
  12.     conn.close()

  13.     vars={
  14.         "title":allfields,
  15.         "data":allrecords
  16.     }

  17.     return psp.PSP(req,"test.psp",vars=vars)

  18. def getstu(req,id):
  19.     req.content_type='text/html'
  20.     #req.content_languages
  21.     #req.content_encoding='gb2312'
  22.     conn=pgdb.connect(host='localhost',database='test',user='test',password='test')
  23.     cur=conn.cursor()
  24.     sql="select * from stu where id='%s'"%str(id[3:])
  25.     cur.execute(sql)
  26.     allfields=cur.description
  27.     allrecords=cur.fetchall()
  28.     cur.close()
  29.     conn.close()
  30.     rstr="%s"%allrecords[0]
  31.     if len(allrecords)>=1:
  32.         rstr='学生详细信息:'+', '.join([str(i) for i in allrecords[0]])
  33.     return rstr
复制代码


test.psp

  1. <html>
  2. <head>
  3. <script src="/styles/prototype-1.4.0.js"></script>
  4. <script>
  5. function getstu(id)
  6. {
  7.     var url='http://192.168.28.9/py/xml/ajax/study/test.py/getstu';
  8.     var pars='id='+id;
  9.     var myAjax=new Ajax.Updater(id,url,{method:'get',parameters:pars});
  10. }
  11. </script>
  12. </head>
  13. <body>
  14. <table width="100%" border="1">
  15. <%
  16. req.write("<tr>")
  17. for i in title:
  18.     req.write("<th>%s</th>"%i[0])
  19. req.write("</tr>")
  20. for i in data:
  21.     req.write('<tr><th><a href=\'javascript:getstu("stu%s");\'>%s</a></th>'%(i[0],i[0]))
  22.     for j in i[1:]:
  23.         req.write("<td>%s</td>"%j)
  24.     req.write("</tr><tr><td colspan='8'><div id='stu%s'></div></td></tr>"%i[0])
  25. #end-for-i
  26. %>
  27. </table>
  28. </body>
  29. </html>
复制代码

[ 本帖最后由 HonestQiao 于 2006-7-7 23:25 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-03-12 09:13 |只看该作者
如果直接在浏览器中调用http://192.168.28.9/py/xml/ajax/study/test.py/getstu?id=stu1能正确显示结果
但使用Ajax就成了乱码,究竟哪里出了问题?

论坛徽章:
0
3 [报告]
发表于 2006-03-21 16:02 |只看该作者
python我不懂。但是ajax在传递参数的时候似乎用的都是utf编码。你需要在后台处理一下

论坛徽章:
0
4 [报告]
发表于 2006-03-22 02:11 |只看该作者
原帖由 bitifirefly 于 2006-3-21 16:02 发表
python但是ajax在传递参数的时候似乎用的都是utf编码


谁说用得都是utf

论坛徽章:
0
5 [报告]
发表于 2006-03-25 22:55 |只看该作者
原帖由 csona 于 2006-3-22 02:11 发表


谁说用得都是utf


那是怎么回事?

论坛徽章:
0
6 [报告]
发表于 2006-04-26 16:40 |只看该作者
我也遇到这样的问题。
1、试这个http://spaces.msn.com/jfwang/行不行。
A、我用editplus来编辑,显示中文是乱码,但用ultraedit就正常显示.

论坛徽章:
0
7 [报告]
发表于 2006-04-26 19:52 |只看该作者
我已经将系统改为utf-8编码,文件用utf-8编码保存,现已一切正常
我的很多网页都需要utf-8编码,所以不能用apache2的AddDefaultCharset这个配置
不过还是谢谢楼上的,这么老的问题还能找出来解答

论坛徽章:
0
8 [报告]
发表于 2006-05-12 00:57 |只看该作者
你也可以不用utf-8编码网页http://ajax.net.cn/blog/?p=17

论坛徽章:
0
9 [报告]
发表于 2006-07-07 14:35 |只看该作者
在server端设置一下字符编码

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
10 [报告]
发表于 2006-07-07 23:26 |只看该作者
或者程序和protype都设置字符集
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP