使用下列py生成动态页面:
#!c:/Python27/python print( "Content-type: text/html" ) print( "" ) #is very important, this line must be here. or you will get error print( '<html><head>' + '<br>' + '</head><body>' + '<p align=center>Hello world.</p>' + '<form name="form1" action=/dynamic/post-query.py method=post>' + 'name: <input type=text name="name" maxlength=10/><br />' + 'age: <input type=text name="age" maxlength=3><br />'+ '<input type=submit value="Confirm"/><input type=reset value="Reset"/><input type=button value="Dafault" onclick=OnDefault()>' + '<script language="javascript" src="form1.js">' + '</script>' + '</form></body></html>' )
通过浏览器访问这个页面,apache将调用python生成html页面如下:
<html><head><br></head><body>
<p align=center>Hello world.</p>
<form name="form1" action=/dynamic/post-query.py method=post>
name: <input type=text name="name" maxlength=10/><br />
age: <input type=text name="age" maxlength=3><br />
<input type=submit value="Confirm"/><input type=reset value="Reset"/><input type=button value="Dafault" onclick=OnDefault()>
<script language="javascript" src="form1.js"></script>
</form>
</body></html>
但是,发现浏览器在点击‘default’按钮使它调用OnDefault()时,发现没有任何的效果。刚开始以为是路径错误,后来改为绝对路径http://127.0.0.4/dynamic/form1.js,还是改为相对路径/dynamic/form1.js都不可以。
实在不行,然后查看apache的error日志,发现有错误:
2] [info] [client 127.0.0.4] mod_wsgi (pid=568, process='', application='127.0.0.4|/dynamic/form1.js'): Loading WSGI script 'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/dynamic/form1.js'., referer: http://127.0.0.4/dynamic/test1.py [Wed Jan 18 13:21:49 2012] [error] [client 127.0.0.4] mod_wsgi (pid=568, process='', application='127.0.0.4|/dynamic/form1.js'): Failed to parse WSGI script file 'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/dynamic/form1.js'., referer: http://127.0.0.4/dynamic/test1.py [Wed Jan 18 13:21:49 2012] [error] [client 127.0.0.4] mod_wsgi (pid=568): Exception occurred processing WSGI script 'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/dynamic/form1.js'., referer: http://127.0.0.4/dynamic/test1.py [Wed Jan 18 13:21:49 2012] [error] [client 127.0.0.4] File "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/dynamic/form1.js", line 1, referer: http://127.0.0.4/dynamic/test1.py [Wed Jan 18 13:21:49 2012] [error] [client 127.0.0.4] function OnDefault(), referer: http://127.0.0.4/dynamic/test1.py [Wed Jan 18 13:21:49 2012] [error] [client 127.0.0.4] ^, referer: http://127.0.0.4/dynamic/test1.py [Wed Jan 18 13:21:49 2012] [error] [client 127.0.0.4] SyntaxError: invalid syntax, referer: http://127.0.0.4/dynamic/test1.py
apache把form1.js也是作为python来执行了,因此浏览器请求这个页面的时候,form1.js没有被download到浏览器端,因此没有任何效果。接下来的问题是为什么apache把form1.js视为python来解析呢? 是因为dynamic这个目录在apache配置文件中被设置为WSGIScriptAlias。所以这下面的所有的文件视为python脚本。
知道原因,解决方案就比较简单了。在和dynamic同级的目录下面新创建一个文件夹,比如说javascript,然后把form1.js丢在那里,把
<script language="javascript" src="form1.js"></script>
改为
<script language="javascript" src="/javascript/form1.js"></script>
这样就可以了。
版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.