安装 sanic-cors

pip install -U sanic-cors

使用

from sanic import Sanic
from sanic.response import json
from sanic_cors import CORS

app = Sanic("Adley")
CORS(app)

@app.route("/api")
async def index(request):
return json({
'code': 20000,
'msg': 'success',
'data': {

}
})

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8088)

【链接】
​​​https://www.cnpython.com/pypi/sanic-cors​