- 论坛徽章:
- 0
|
看了limodou的教程,我在网上找了一份prototype.js试了一下,发现它有个Ajax.Updater用来更新html的挺好用,但是有点小问题, Ajax.Updater从服务器端返回页面的编码是gb2312,但是主页面更新了之后却成了乱码,不知道哪位能帮助解决这个问题
ps:
我的服务器用的是mod_python,用了2个文件test.py和test.psp
test.py
- import pgdb
- from mod_python import psp,apache,util
- def index(req):
- req.content_type='text/html'
- conn=pgdb.connect(host='localhost',database='test,user='test',password='test')
- cur=conn.cursor()
- sql="select * from schstu"
- cur.execute(sql)
- allfields=cur.description
- allrecords=cur.fetchall()
- cur.close()
- conn.close()
- vars={
- "title":allfields,
- "data":allrecords
- }
- return psp.PSP(req,"test.psp",vars=vars)
- def getstu(req,id):
- req.content_type='text/html'
- #req.content_languages
- #req.content_encoding='gb2312'
- conn=pgdb.connect(host='localhost',database='test',user='test',password='test')
- cur=conn.cursor()
- sql="select * from stu where id='%s'"%str(id[3:])
- cur.execute(sql)
- allfields=cur.description
- allrecords=cur.fetchall()
- cur.close()
- conn.close()
- rstr="%s"%allrecords[0]
- if len(allrecords)>=1:
- rstr='学生详细信息:'+', '.join([str(i) for i in allrecords[0]])
- return rstr
复制代码
test.psp
- <html>
- <head>
- <script src="/styles/prototype-1.4.0.js"></script>
- <script>
- function getstu(id)
- {
- var url='http://192.168.28.9/py/xml/ajax/study/test.py/getstu';
- var pars='id='+id;
- var myAjax=new Ajax.Updater(id,url,{method:'get',parameters:pars});
- }
- </script>
- </head>
- <body>
- <table width="100%" border="1">
- <%
- req.write("<tr>")
- for i in title:
- req.write("<th>%s</th>"%i[0])
- req.write("</tr>")
- for i in data:
- req.write('<tr><th><a href=\'javascript:getstu("stu%s");\'>%s</a></th>'%(i[0],i[0]))
- for j in i[1:]:
- req.write("<td>%s</td>"%j)
- req.write("</tr><tr><td colspan='8'><div id='stu%s'></div></td></tr>"%i[0])
- #end-for-i
- %>
- </table>
- </body>
- </html>
复制代码
[ 本帖最后由 HonestQiao 于 2006-7-7 23:25 编辑 ] |
|