flask:flask_cors模块解决前端请求跨域问题
1 代码如下:
# -*- coding: utf-8 -*- from __future__ import print_function from flask import Flask from flask import Flask, jsonify, request, abort from flask_cors import CORS app = Flask(__name__) CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True) # docker-config app.config['MONGODB_SETTINGS'] = { 'host': 'mongodb://mongodb/datasets', 'port': 27017, 'connect': False } # docker-config # app.config['MONGODB_SETTINGS'] = { # 'host': 'mongodb://mongodb', # 'port': 27017, # 'db': 'datasets' # } # localhost-config # app.config['MONGODB_SETTINGS'] = { # 'host': 'localhost', # 'port': 27017, # 'db': 'datasets' # } app.debug = True from models import db db.init_app(app) if __name__ == "__main__": app.run(debug=True)
2 simple install
(1) pip install flask-cors
(2) from flask_cors import CORS
(3) 在新创建的app后面CORS(app, resources={r"/*": {"origins": "*"}}, supports_credentials=True)