-1

I am getting this error while connecting to my Flask app using socket.io

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.

I tried this solution

@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

But it did not work. I also tried Flask-Cors module

CORS(app, resources={
    r'/*/*': {
        'origins': '*',
        'allow_headers': ['Content-Type', 'Authorization']
    }
})

and no gain. Any help would be appreciated

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60

1 Answers1

0

It was socket.io-client library issue. I was using v1.3.7 as suggested by Flask-Socket.IO docs but it did not work but changing the version for socket.io-client.js it worked fine.

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.

But I don't know why it did not worked for me as I was using Flask-SocketIO==1.2 and socket.io-client@v1.3.7

Community
  • 1
  • 1
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60