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,
- I still get the same javascript error as above
- The error-function in the ajax call is used (I get a popup saying 'An error occurred')
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
.