Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to use Python, AJAX & CGI together
    primarykey
    data
    text
    <p><strong>I want to have a web page where you click a button, by using AJAX I get a string from a python script, then display that string in a paragraph HTML element.</strong></p> <p><strong>I know I can do this by using Python, WSGI &amp; AJAX (theoretically I can do it this way) but its waaaay too difficult. I am experienced with CGI &amp; python.</strong></p> <p><strong>So can I do the above using CGI?</strong></p> <p>If I can how do the python script work, exactly the same as when serving a page using CGI?</p> <p>This doesn't work:</p> <pre><code>import cgitb; cgitb.enable() import cgi import os print "Content-Type: text/html\n" input_data = cgi.FieldStorage() print "hello" </code></pre> <p>When I click my button in my page, nothing happens &amp; my CGI server (which works fine for cgi page requests) gives me a http 501 error.</p> <p>My html &amp; javascript:</p> <pre><code> &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;script type="text/javascript"&gt; &lt;!-- function onTest( dest, params ) { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { document.getElementById( "bb" ).innerHTML = xmlhttp.responseText; } } xmlhttp.open("POST",dest,true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send( params ); } --&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;p id="bb"&gt; abcdef &lt;/p&gt; &lt;a href="javascript:onTest('aaa.py', '')"&gt;Click it&lt;/a&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload