Flask--跨域
发布日期:2021-05-14 12:20:48 浏览次数:21 分类:精选文章

本文共 896 字,大约阅读时间需要 2 分钟。

文章目录

跨域配置

  • 方法一: 设置CORS策略
from flask import Flask, render_templatefrom flask_cors import CORSapp = Flask(__name__)# 配置跨域: supports_credentials=True, 必须配置,否则不生效# resources = r'/*': 让本服务器所有的URL都允许跨域请求,# resources = {r"/api": {"origins": "*"}}: 让本服务器/api开头的URL允许跨域请求CORS(app, resources=r'/*', supports_credentials=True)@app.route('/', methods=['get'])def index():        return render_template('index.html')if __name__ == "__main__":    app.run()

方法二:对请求的Response header中加入header

from flask import Flask, render_templateapp = Flask(__name__)@app.after_requestdef 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
上一篇:WEB--js点击事件写法
下一篇:WEB--Ajax基础

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月18日 19时38分49秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章