Flask-Socket.IO CORS 问题
Flask-Socket.IO CORS issue
我在使用 socket.io
连接到我的 Flask 应用程序时遇到此错误
GET http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=1450525025659-286 Request.create @ socket.io.js:2919Request @ socket.io.js:2842XHR.request @ socket.io.js:2773XHR.doPoll @ socket.io.js:2803Polling.poll @ socket.io.js:3192Polling.doOpen @ socket.io.js:3136Transport.open @ socket.io.js:2313Socket.open @ socket.io.js:1743Socket @ socket.io.js:1625Socket @ socket.io.js:1560Manager.open.Manager.connect @ socket.io.js:299(anonymous function) @ socket.io.js:564
:3004/:1 XMLHttpRequest cannot load http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=1450525025659-286. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3004' is therefore not allowed access. The response had HTTP status code 500.
我试过这个解决方案
@app.after_request
def add_cors(resp):
resp.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin','*')
resp.headers['Access-Control-Allow-Credentials'] = 'true'
resp.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS, GET'
resp.headers['Access-Control-Allow-Headers'] = request.headers.get(
'Access-Control-Request-Headers', 'Authorization')
return resp
但是没有用。
我也试过 Flask-Cors 模块
CORS(app, resources={
r'/*/*': {
'origins': '*',
'allow_headers': ['Content-Type', 'Authorization']
}
})
并没有收获。
任何帮助将不胜感激
这是 socket.io-client
库问题。我正在按照 Flask-Socket.IO 文档的建议使用 v1.3.7 但它不起作用,但更改了 [=21 的版本=]socket.io-client.js 效果很好。
http://flask-socketio.readthedocs.org/en/latest/#upgrading-to-flask-socketio-1-x-from-older-releases
Upgrading to Flask-SocketIO 1.x from older releases
On the client side, you need to upgrade your Socket.IO Javascript client from the 0.9.x releases to the 1.3.x or newer releases.
但我不知道为什么它对我不起作用,因为我正在使用 Flask-SocketIO==1.2
和 socket.io-client@v1.3.7
我在使用 socket.io
连接到我的 Flask 应用程序时遇到此错误GET http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=1450525025659-286 Request.create @ socket.io.js:2919Request @ socket.io.js:2842XHR.request @ socket.io.js:2773XHR.doPoll @ socket.io.js:2803Polling.poll @ socket.io.js:3192Polling.doOpen @ socket.io.js:3136Transport.open @ socket.io.js:2313Socket.open @ socket.io.js:1743Socket @ socket.io.js:1625Socket @ socket.io.js:1560Manager.open.Manager.connect @ socket.io.js:299(anonymous function) @ socket.io.js:564 :3004/:1 XMLHttpRequest cannot load http://127.0.0.1:5000/socket.io/?EIO=3&transport=polling&t=1450525025659-286. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3004' is therefore not allowed access. The response had HTTP status code 500.
我试过这个解决方案
@app.after_request
def add_cors(resp):
resp.headers['Access-Control-Allow-Origin'] = request.headers.get('Origin','*')
resp.headers['Access-Control-Allow-Credentials'] = 'true'
resp.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS, GET'
resp.headers['Access-Control-Allow-Headers'] = request.headers.get(
'Access-Control-Request-Headers', 'Authorization')
return resp
但是没有用。 我也试过 Flask-Cors 模块
CORS(app, resources={
r'/*/*': {
'origins': '*',
'allow_headers': ['Content-Type', 'Authorization']
}
})
并没有收获。 任何帮助将不胜感激
这是 socket.io-client
库问题。我正在按照 Flask-Socket.IO 文档的建议使用 v1.3.7 但它不起作用,但更改了 [=21 的版本=]socket.io-client.js 效果很好。
http://flask-socketio.readthedocs.org/en/latest/#upgrading-to-flask-socketio-1-x-from-older-releases
Upgrading to Flask-SocketIO 1.x from older releases
On the client side, you need to upgrade your Socket.IO Javascript client from the 0.9.x releases to the 1.3.x or newer releases.
但我不知道为什么它对我不起作用,因为我正在使用 Flask-SocketIO==1.2
和 socket.io-client@v1.3.7