I have this server in python:
class MyServer(SimpleHTTPRequestHandler):
def do_POST(self):
...
some code here
...
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'POST')
self.send_header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers')
self.end_headers()
self.send_response(200)
if __name__ == "__main__":
webServer = HTTPServer((hostName, serverPort), MyServer)
print("Server started http://%s:%s" % (hostName, serverPort))
try:
webServer.serve_forever()
except KeyboardInterrupt:
pass
webServer.server_close()
print("Server stopped.")
But when I try to make a request in my js code in chrome, I get this error: POST http://localhost:8080/ net::ERR_INVALID_HTTP_RESPONSE
I have read that it is cors problem and I need to add the headers Access-Control-Allow-Origin
, and I did and it didn't help.
Anyone has any idea how to fix it?
You need to call send_response
first, then your custom send_header
calls, then end_headers
, then send whatever data you are sending. SimpleHTTPRequestHandler is extremely primitive, and expects you to understand the HTTP protocol.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.