0
我是網絡開發新手,所以讓我解釋一下:
我想讓我的Python Tornado服務器與網頁進行通信。我的網頁使用WebSockets和onmessage
函數來打印它應該從Tornado服務器接收的內容。基本上,這裏是JavaScript的HTML部分:Python龍捲風:我如何設置WebSocket標題?
$(document).ready(function() {
var myURL = "http://localhost:8888";
var source = new EventSource(myURL, { withCredentials: true }); // Access-Control-Allow-Origin
...
source.onmessage = function(event) {
console.log("received new event!");
};
...
}); // ready()
我設置withCredentials
參數true
所以CORS被啓用。
在龍捲風方面,我有一個WebSocket類,應該回答,但我不知道如何設置標題Access-Control-Allow-Origin
啓用。這裏是龍捲風代碼:
class EchoWebSocket(tornado.websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def on_message(self, message):
self.write_message(u"Received message: " + message)
def make_app():
return tornado.web.Application([ ('/', EchoWebSocket), ])
if __name__ == '__main__':
app = make_app()
app.listen(8888)
print 'listening on port 8888...'
# start main loop
tornado.ioloop.IOLoop.current().start()
我在我的瀏覽器中出現以下錯誤!
GET http://localhost:8888/ [HTTP/1.1 400 Bad Request 1ms]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8888/. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
我失蹤了什麼?