python - Jquery AJAX calling is not returing success -
i novice , written code using jquery display output of python program on browser 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 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 load html code in browser below alert messages in sequence:
first alert message:
this 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 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 python code , 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 not able achieve objective render html output of python script browser?
==================================================================================
i downloaded firebug , below logs 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 html file there
please advise.
==================================================================================
!!!!!solved!!!!!
the issue was using ajax url construct wrongly:
url: "http://xx.xx.xx.xx/~prakash_prasad/project/runreport.py",
when changed :
url: "runreport.py",
it worked perfectly
please advise difference via above 2 ajax url call via jquery - though doamin same python script , html file?
you need 2 things:
first, add print "200 ok"
before print out content-type. second, make sure sending on two blank lines if going building entire responses yourself:
print '200 ok' # status codes helpful # 2 blank lines between headers , body 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>'
Comments
Post a Comment