0

Following the example given here I am trying to setup a simple client/webserver to use javascript as client and python as server. However, I seem to do something wrong, as I do not get an output on the server side (and no error message in the javascript console).

The complete files are exampleClient.html:

<html>
<body>
<script src="jquery.js"></script>
<script>
    function toPython(usrdata){
    $.ajax({
        url: "localhost:8082",
        type: "POST",
        data: { information : "You have a very nice website, sir." , userdata : usrdata },
        dataType: "json",
        success: function(data) {
           //s <!-- do something here -->
            $('#somediv').html(data);
        }});
    }
    $("#filter").click(toPython("something5"));
    //$("#onclick").bind('click', toPython("something5"));


</script>

<input type="button" id="filter" name="filter" value="Filter" />
<p id="demo"></p>


</body>
</html>

and server.py:

# Python and Gevent
from gevent.pywsgi import WSGIServer
from gevent import monkey
monkey.patch_all() # makes many blocking calls asynchronous

def application(environ, start_response):
    if environ["REQUEST_METHOD"]!="POST": # your JS uses post, so if it isn't post, it isn't you
        start_response("403 Forbidden", [("Content-Type", "text/html; charset=utf-8")])
        return "403 Forbidden"
    start_response("200 OK", [("Content-Type", "text/html; charset=utf-8")])
    r = environ["wsgi.input"].read() # get the post data
    print(r)
    return r

address = "localhost", 8082
server = WSGIServer(address, application)
server.backlog = 256
server.serve_forever()

I would appreciate help to fix the problem, or if you have another working example of how to create such a client/server setup...

Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Try putting the script at the end of the body. i.e. after

    tag.

    – Shreyash S Sarnayak Jul 23 '17 at 13:51
  • @ShreyashSSarnayak: Thanks for the suggestion, but no change – Alex Jul 23 '17 at 13:59
  • Try to use a protocol in javascript `url: "http://localhost:8082"` – Maurice Meyer Jul 23 '17 at 14:32
  • @MauriceMeyer: It kind of works, but not in the way intended. I start the server, then I Load the html (without clicking on it) and then I get an error I cannot paste here. Maybe you have an idea on where I can find a REAL and WORKING example? – Alex Jul 24 '17 at 08:45

0 Answers0