2012-03-11 91 views
1

我是新手和使用jQuery顯示Python程序上的瀏覽器輸出到用戶編寫代碼:jQuery的AJAX調用不returing成功

<!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輸出呈現給瀏覽器的目標?

============================================== ====================================

我下載螢火以下是我看到的日誌in 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 2阿賈克斯URL調用 - 儘管doamin是同爲Python腳本和HTML文件?

回答

1

你需要做兩件事情:

首先,前加print "200 OK"你打印出來的內容類型。二,請確保您要發送超過2個空行,如果你將要建立整個反應自己:

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>' 
+0

我想你的方法,但同樣的問題之前, – Prakash 2012-03-11 13:04:58