我是一名新手并使用Jquery编写代码,以便在浏览器上向用户显示python程序的输出:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Project</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link type="text/css" href="jquery/css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jquery/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery/js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#runReport').click(function() {
runreport($('#starttime').val(), $('#endtime').val());
});
function runreport(startdate, enddate) {
$('.date').html('<img src=img/load.gif alt=loading.../>');
alert ('this is a test ' + startdate + ' enddate ' + enddate);
$.ajax({
type: "GET",
url: "http://xx.xx.xx.xx/~prakash_prasad/project/runreport.py",
data: {
'startTime': startdate,
'endTime': enddate
},
success: function(result) {
alert ('Success' );
$("p").html(result);
},
error:function(xhr,err) {
alert("Failed\nreadyState: "+xhr.readyState+"\nstatus: "+xhr.status + "\nresponseText: "+xhr.responseText);
}
});
}
});
$(document).ready(function(){
$('.date').datepicker({
flat: true,
date: '2012-03-10',
current: '2012-03-10',
calendars: 1,
starts: 1
});
});
</script>
</head>
<body>
Start Date : <input type="text" name="startdate" id="starttime" class="date"/>
End Date : <input type="text" name="enddate" id="endtime" class="date"/>
<button id="runReport">Run Report</button>
<p></p>
</body>
</html>
当我在浏览器中加载此html代码时,我会按顺序获得以下警告消息:
第一条提醒讯息:
this is a test 03/10/2012 enddate 03/30/2012
第二条警告信息:
Failed
readyState: 0
status: 0
responseText:
runreport.py代码是::
#!/usr/local/bin/python
import cgi
import MySQLdb
from xml.dom.minidom import Document
import json
print 'Content-Type: text/html\n'
print '<html><body>'
form = cgi.FieldStorage()
starttime = form["startTime"].value
endtime = form["endTime"].value
print '<b>%s</b>' % (starttime)
print '<b>%s</b>' % (endtime)
print '</body></html>'
我运行了我的python代码并且工作正常。
project>$ curl "http://xx.xx.xx.xx/~prakash_prasad/project/runreport.py?startTime=2011-03-09&endTime=2012-03-07"
<html><body>
<b>2011-03-09</b>
<b>2012-03-07</b>
</body></html>
为什么我无法实现将我的python脚本的html输出呈现给浏览器的目标?
=============================================== ===================================
我下载了Firebug,下面是我在Net Tab中看到的日志:
Headers
Response Headersview source
Connection Keep-Alive
Content-Type text/plain
Date Sun, 11 Mar 2012 17:08:45 GMT
Keep-Alive timeout=5, max=100
Server Apache/2.2.17 (Unix) PHP/5.3.3 mod_wsgi/3.3 Python/2.7.1 mod_jk/1.2.31 mod_perl/2.0.5 Perl/v5.8.8
Transfer-Encoding chunked
Request Headersview source
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Host xx.xx.xx.xx
Origin http://tools
Referer http://tools/~prakash_prasad/project/p.html
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
http://xx.xx.xx.xx/~prakash_prasad/project/runreport.py?startTime=03%2F09%2F2012&endTime=03%2F23%2F2012 Size 64B Status 200 OK Domain same host where my HTML file is there
请告知。
=============================================== ===================================
!!!!!解决!!!!!
问题是我错误地使用了AJAX URL构造:
url: "http://xx.xx.xx.xx/~prakash_prasad/project/runreport.py",
当我将其更改为:
url: "runreport.py",
完美无缺
请通过Jquery通过以上两个Ajax URL调用告知有什么区别 - 虽然doamin对于python脚本和html文件是一样的吗?
答案 0 :(得分:1)
你需要做两件事:
首先,在打印Content-Type之前添加print "200 OK"
。其次,如果您要自己构建完整的响应,请确保通过两个空行发送:
print '200 OK' # Status codes are helpful
# Two blank lines between headers and body are required
print 'Content-Type: text/html\n\n'
print '<html><body>'
form = cgi.FieldStorage()
starttime = form["startTime"].value
endtime = form["endTime"].value
print '<b>%s</b>' % (starttime)
print '<b>%s</b>' % (endtime)
print '</body></html>'