Note that there are some explanatory texts on larger screens.

plurals
  1. POWSGI: Use AJAX to get a string from a python script
    primarykey
    data
    text
    <p>I am delving into WSGI &amp; it is pretty difficult. </p> <p><strong>What I am trying to do is something pretty simple: when I click a link I want to get the string "hello" from a python script &amp; display "hello" in my HTML paragraph element.</strong></p> <p>Now I have made WSGI python scripts that display HTML text, ie, serve a page using WSGI python, but not used Python, AJAX &amp; WSGI together for the above mentioned purpose.</p> <p><em>Right now what happens is when I click the link in my HTML page, the paragraph element displays "error" not "hello". Where do you think I am going wrong; in python or javascript?</em></p> <p>Is my python script below correct?:</p> <pre><code>#!/usr/bin/env python from wsgiref.simple_server import make_server from cgi import parse_qs, escape def application(environ, start_response): return [ "hello" ] if __name__ == '__main__': from wsgiref.simple_server import make_server srv = make_server('localhost', 8000, application) srv.serve_forever() </code></pre> <p>Maybe its my Javascript &amp;/or HTML where I am wrong?:</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> <p>Steps I take to test it:<br> - Run wsgi fake server script<br> - open browser &amp; type <a href="http://localhost:8000/test.html" rel="nofollow">http://localhost:8000/test.html</a><br> - click link in html page &amp; get "error" back </p> <p>My files wsgi.py, aaa.py &amp; test.html are all in the same folder.</p> <p>My Server: import threading import webbrowser import os from wsgiref.simple_server import make_server</p> <pre><code>FILE = 'index.html' PORT = 8000 def test_app(environ, start_response): if environ['REQUEST_METHOD'] == 'POST': try: request_body_size = int(environ['CONTENT_LENGTH']) request_body = environ['wsgi.input'].read(request_body_size) except (TypeError, ValueError): request_body = "0" try: response_body = str(int(request_body) ** 2) except: response_body = "error" status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) return [response_body] else: f = environ['PATH_INFO'].split( "?" )[0] f = f[1:len(f)] response_body = open(f).read() status = '200 OK' headers = [('Content-type', 'text/html'), ('Content-Length', str(len(response_body)))] start_response(status, headers) return [response_body] def open_browser(): """Start a browser after waiting for half a second.""" def _open_browser(): webbrowser.open('http://localhost:%s/%s' % (PORT, FILE)) thread = threading.Timer(0.5, _open_browser) thread.start() def start_server(): """Start the server.""" httpd = make_server("", PORT, test_app) httpd.serve_forever() if __name__ == "__main__": open_browser() print "Now serving on Port 8000" start_server() </code></pre>
    singulars
    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.
    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