<!DOCTYPE html>
<html>
<script src="https://d3js.org/d3.v3.js"></script>
<script>
function login(){
var params = window.location.search;
d3.xhr("http://localhost:8000/login")
.get(function(err, data){
console.log(data);
});
}
</script>
<body onload="login()">
<p id="params"></p>
</body>
</html>
The python code is below. When I run it with python3 server.py, the html loads and does GET on the /login and then the browser gives an error:
Failed to load http://example.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
What am I doing wrong?
import tornado.ioloop
import tornado.web
import json
import os
R_URL='http://example.com'
class MainHandler(tornado.web.RequestHandler):
def set_default_headers(self):
print("setting headers!!!")
self.set_header("access-control-allow-origin", "*")
self.set_header('Access-Control-Allow-Methods', 'GET, PUT, DELETE, OPTIONS')
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Headers",
"Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, Cache-Control")
def get(self):
print("GET: MainHandler::localhost")
self.redirect(R_URL)
#self.get(R_URL)
def post(self):
print("Received POST")
self.redirect(R_URL)
def make_app():
root = os.path.dirname(__file__)
print("root", root)
application = tornado.web.Application([
(r"/login", MainHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": root, "default_filename": "index.html"}),
])
return application
if __name__ == "__main__":
app = make_app()
app.listen(8000)
print("Listening on port 8000")
tornado.ioloop.IOLoop.current().start()