javascript/python client/webserver example not working

风流意气都作罢 提交于 2019-12-11 16:10:02

问题


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

来源:https://stackoverflow.com/questions/45265760/javascript-python-client-webserver-example-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!