现在web开发已经进入前后端分离的阶段
后端往往只需要吐api数据就ok
一般纯的api接口需要考虑跨域访问问题
下面是简单的跨域访问装饰器在flask中的实现
from functools import wraps from flask import make_response def allow_cross_domain(fun): @wraps(fun) def wrapper_fun(*args, **kwargs): rst = make_response(fun(*args, **kwargs)) rst.headers['Access-Control-Allow-Origin'] = '*' rst.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE' allow_headers = "Referer,Accept,Origin,User-Agent" rst.headers['Access-Control-Allow-Headers'] = allow_headers return rst return wrapper_fun @app.route('/hosts/') @allow_cross_domain def domains(): pass