1

I am a novice and written a code using Jquery to display an output of a python program on a browser to a user:

<!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>

When I load this html code in browser I get the below alert messages in sequence:

First alert message:

this is a test 03/10/2012 enddate 03/30/2012

Second alert message:

Failed
readyState: 0
status: 0
responseText: 

The runreport.py code is::

#!/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>'

I had ran my python code and it works fine.

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>

Why am I not able to achieve the objective to render html output of my python script to browser?

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

I downloaded Firebug and below is the logs I see 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

Please advise.

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

!!!!!SOLVED!!!!!

The issue was I was using the AJAX URL construct wrongly:

url:  "http://xx.xx.xx.xx/~prakash_prasad/project/runreport.py",

When I changed it to :

url:  "runreport.py",

It worked perfectly

Please advise what is the difference via above two Ajax URL call via Jquery - though doamin is same for the python script and html file?

1 Answer 1

1

You need to do two things:

First, add print "200 OK" before you print out the Content-Type. Second, make sure that you are sending over two blank lines if you are going to be building entire responses yourself:

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>'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.