先後端分離在開發調試階段本地的flask測試服務器須要容許跨域訪問,簡單解決辦法有二:flask
pip install flask_cors
from flask_cors import CORS app = Flask(__name__) CORS(app, supports_credentials=True) if __name__ == "__main__": app.run()
@app.after_request def af_request(resp): """ #請求鉤子,在全部的請求發生後執行,加入headers。 :param resp: :return: """ resp = make_response(resp) resp.headers['Access-Control-Allow-Origin'] = '*' resp.headers['Access-Control-Allow-Methods'] = 'GET,POST' resp.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type' return resp