下面的Javascript试图将数据发送到python服务器,并试图根据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).
但我仍然得到相同的错误,但数据似乎是发送到服务器时,我按下按钮。但是
在python方面,我得到以下输出和错误:
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
html的完整代码如下:
<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>
下面是服务器端的python代码:
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()
如果您能提供一个完整的和工作的示例,说明如何将数据从javascript客户机发送到python服务器,以及其他方法,我将不胜感激。你知道吗
为了用three.js
创建图形,我必须在客户端/web端使用javascript。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐