Mootools Request Receive "501 Unsupported Method (" OPTIONS ") response

I have this mootools request:

new Request({ url: 'http://localhost:8080/list', method: 'get', }).send(); 

and a small python server that processes it like this:

 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import subprocess class HttpHandler(BaseHTTPRequestHandler): def do_GET(self): if self.path == '/list': self.list() else: self._404() def list(self): self.response200() res = "some string" self.wfile.write(res) def _404(self): self.response404() self.wfile.write("404\n") def response200(self): self.send_response(200) self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Headers', 'X-Request, X-Requested-With') self.send_header('Content-type', 'application/json') self.end_headers() def response404(self): self.send_response(404) self.send_header('Content-type', 'application/json') self.end_headers() def main(): try: server = HTTPServer(('', 8080), HttpHandler) server.serve_forever() except KeyboardInterrupt: server.socket.close() if __name__ == '__main__': main() 

When I try to execute this request, I get the following errors:

 OPTIONS http://localhost:8080/ 501 (Unsupported method ('OPTIONS')) XMLHttpRequest cannot load http://localhost:8080/. Origin null is not allowed by Access-Control-Allow-Origin. 

I'm not sure what is going on. Can someone help me?

+2
source share
1 answer

exactly the same as the response line tells you: OPTIONS http://localhost:8080/ 501 (Unsupported method ('OPTIONS'))

When javascript tries to request a resource from another source, modern browsers first set a different server, the goal, if it is normal to make this request from another source, is exactly what Access-Control* headers do. but this request is not executed in a regular GET , as it will actually execute the request anyway and instead uses the OPTIONS method, which exists for the sole reason to inform clients that they are allowed to do this.

So, you need a do_OPTIONS method, which might look something like this:

 def do_OPTIONS(self): if self.path in ('*', '/list'): self.send_response(200) self.send_header('Allow', 'GET, OPTIONS') self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Headers', 'X-Request, X-Requested-With') else: self.send_response(404) self.send_header('Content-Length', '0') self.end_headers() 
+4
source

Source: https://habr.com/ru/post/1481130/


All Articles