0

I have the following Javascript trying to send data to a python server and trying to fix an error according to this question

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8082/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

But I still get the same error, but the data seems to be sent to the server when I pressed the button. However,

  1. I still get the same javascript error as above
  2. The error-function in the ajax call is used (I get a popup saying 'An error occurred')
  3. On the python side I get the following output and error:

    b'time=00%3A00%3A00.00&date=zfgeztrze'
    Traceback (most recent call last):
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 935, in handle_one_response
        self.run_application()
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 909, in run_application
        self.process_result()
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 895, in process_result
        self.write(data)
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 741, in write
        self._write_with_headers(data)
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 746, in _write_with_headers
        self.finalize_headers()
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 689, in finalize_headers
        total_len = sum(len(chunk) for chunk in self.result)
      File "/usr/local/lib/python3.4/dist-packages/gevent/pywsgi.py", line 689, in <genexpr>
        total_len = sum(len(chunk) for chunk in self.result)
    TypeError: object of type 'int' has no len()
    Mon Jul 24 11:03:16 2017 {'HTTP_HOST': 'localhost:8082', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '54480', (hidden keys: 25)} failed with TypeError
    
    127.0.0.1 - - [2017-07-24 11:03:16] "POST / HTTP/1.1" 500 21 0.002378
    

The full code of the html is here:

<html>
  <head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
      $(function () {

        $('form').on('submit', function (e) {

          e.preventDefault();

          $.ajax({
            type : "POST",
            processData:false,
            crossDomain:true,
            crossOrigin:true,
            contentType:false,
            url: 'http://localhost:8082',
            data: $('form').serialize(),
            header:{'Access-Control-Allow-Origin': '*'},
            success: function(data) {
                alert('Horray the AJAX call succeeded!');
            },
            error: function(xhr, error) {
                alert('An error occurred.');
            }
          });

        });

      });
    </script>
  </head>
  <body>
    <form>
      <input name="time" value="00:00:00.00"><br>
      <input name="date" value="0000-00-00"><br>
      <input name="submit" type="submit" value="Submit">
    </form>
  </body>
</html>

And here is the python code on the server side:

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()

Instead of answering this question I also would appreciate if you could provide a COMPLETE and WORKING example on how to send data from the javascript-client to the python server, and the other way around.

I really have to use javascript on the client/web side in order to create graphics with three.js.

Alex
  • 41,580
  • 88
  • 260
  • 469
  • You would make life a lot easier for yourself if you used an actual web framework like Flask, instead of having to write raw WSGI functions. – Daniel Roseman Jul 24 '17 at 09:15
  • I am looking for something easy and simple.I don't want to learn a new framework. But if the easiest way is to make it more complicated: Can you provide a FULL and EASY and WORKING(!) example then, which I can have up and running in 15 minutes? – Alex Jul 24 '17 at 09:20
  • @DanielRoseman: Ah flask is for python. I have it installed. – Alex Jul 24 '17 at 09:22
  • http://flask.pocoo.org/docs/0.12/quickstart/ . Note, you are not actually doing any cross-domain requests, so you *don't need* any of that extra configuration. – Daniel Roseman Jul 24 '17 at 09:22
  • What is a cross-domain request? – Alex Jul 24 '17 at 09:23
  • And the quickstart page only contains python code. What does the javascript code look like? – Alex Jul 24 '17 at 09:24
  • Your JS code is already fine, except you don't need the `crossDomain`, `crossOrigin`, and the `Access-Control-Allow-Origin` header. All this is for making Ajax requests to a domain that is not the same as the one that originally served the HTML page, which is not something you are doing. – Daniel Roseman Jul 24 '17 at 09:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/149972/discussion-between-alex-and-daniel-roseman). – Alex Jul 24 '17 at 09:30
  • The basic example of flask works, but how to send data to and from the javascript? I cannot really see how to proceed now. – Alex Jul 24 '17 at 09:34

0 Answers0