flask-cors
https://github.com/corydolphin/flask-cors/blob/master/examples/app_based_example.py
""" | |
Flask-Cors example | |
=================== | |
This is a tiny Flask Application demonstrating Flask-Cors, making it simple | |
to add cross origin support to your flask app! | |
:copyright: (C) 2013 by Cory Dolphin. | |
:license: MIT/X11, see LICENSE for more details. | |
""" | |
from flask import Flask, jsonify | |
import logging | |
try: | |
from flask.ext.cors import CORS # The typical way to import flask-cors | |
except ImportError: | |
# Path hack allows examples to be run without installation. | |
import os | |
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
os.sys.path.insert(0, parentdir) | |
from flask.ext.cors import CORS | |
app = Flask('FlaskCorsAppBasedExample') | |
logging.basicConfig(level=logging.INFO) | |
# To enable logging for flask-cors, | |
logging.getLogger('flask_cors').level = logging.DEBUG | |
# One of the simplest configurations. Exposes all resources matching /api/* to | |
# CORS and allows the Content-Type header, which is necessary to POST JSON | |
# cross origin. | |
CORS(app, resources=r'/api/*', allow_headers='Content-Type') | |
@app.route("/") | |
def helloWorld(): | |
''' | |
Since the path '/' does not match the regular expression r'/api/*', | |
this route does not have CORS headers set. | |
''' | |
return ''' | |
<html> | |
<h1>Hello CORS!</h1> | |
<h3> End to end editable example with jquery! </h3> | |
<a class="jsbin-embed" href="http://jsbin.com/zazitas/embed?js,console">JS Bin on jsbin.com</a> | |
<script src="//static.jsbin.com/js/embed.min.js?3.35.12"></script> | |
</html> | |
''' | |
@app.route("/api/v1/users/") | |
def list_users(): | |
''' | |
Since the path matches the regular expression r'/api/*', this resource | |
automatically has CORS headers set. The expected result is as follows: | |
$ curl --include -X GET http://127.0.0.1:5000/api/v1/users/ \ | |
--header Origin:www.examplesite.com | |
HTTP/1.0 200 OK | |
Access-Control-Allow-Headers: Content-Type | |
Access-Control-Allow-Origin: * | |
Content-Length: 21 | |
Content-Type: application/json | |
Date: Sat, 09 Aug 2014 00:26:41 GMT | |
Server: Werkzeug/0.9.4 Python/2.7.8 | |
{ | |
"success": true | |
} | |
''' | |
return jsonify(user="joe") | |
@app.route("/api/v1/users/create", methods=['POST']) | |
def create_user(): | |
''' | |
Since the path matches the regular expression r'/api/*', this resource | |
automatically has CORS headers set. | |
Browsers will first make a preflight request to verify that the resource | |
allows cross-origin POSTs with a JSON Content-Type, which can be simulated | |
as: | |
$ curl --include -X OPTIONS http://127.0.0.1:5000/api/v1/users/create \ | |
--header Access-Control-Request-Method:POST \ | |
--header Access-Control-Request-Headers:Content-Type \ | |
--header Origin:www.examplesite.com | |
>> HTTP/1.0 200 OK | |
Content-Type: text/html; charset=utf-8 | |
Allow: POST, OPTIONS | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Headers: Content-Type | |
Access-Control-Allow-Methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT | |
Content-Length: 0 | |
Server: Werkzeug/0.9.6 Python/2.7.9 | |
Date: Sat, 31 Jan 2015 22:25:22 GMT | |
$ curl --include -X POST http://127.0.0.1:5000/api/v1/users/create \ | |
--header Content-Type:application/json \ | |
--header Origin:www.examplesite.com | |
>> HTTP/1.0 200 OK | |
Content-Type: application/json | |
Content-Length: 21 | |
Access-Control-Allow-Origin: * | |
Server: Werkzeug/0.9.6 Python/2.7.9 | |
Date: Sat, 31 Jan 2015 22:25:04 GMT | |
{ | |
"success": true | |
} | |
''' | |
return jsonify(success=True) | |
if __name__ == "__main__": | |
app.run(debug=True) |
flask-cors的更多相关文章
- flask跨域问题
在Flask开发RESTful后端时,前端请求会遇到跨域的问题.下面是解决方法: 使用 flask-cors库可以很容易的解决 1 pip install flask-cors 两种方法,一个是全 ...
- 七月小说网 Python + GraphQL (三)
概述 后台数据库几个基本表基本搭建完毕,看了下Github Develop的V4 Api抛弃了RESTful,采用GraphQL,感觉很有意思,一看文档,竟然有Python的开源实现 Graphene ...
- tornado django flask 跨域解决办法(cors)
XMLHttpRequest cannot load http://www.baidu.com. No 'Access-Control-Allow-Origin' header is present ...
- 使用CORS解决flask前端页面跨域问题
from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route(" ...
- Flask配置Cors跨域
1 跨域的理解 跨域是指:浏览器A从服务器B获取的静态资源,包括Html.Css.Js,然后在Js中通过Ajax访问C服务器的静态资源或请求.即:浏览器A从B服务器拿的资源,资源中想访问服务器C的资源 ...
- flask之CORS跨域请求处理
from flask import Flask from flask_cors import CORS#pip install Flask-CORS#跨域请求模块 app = Flask(__name ...
- angularjs flask跨域问题 XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin'
场景,我要来我的server(A)上用api来访问另一个server(B)的问题,如果直接在A上调用B的api,那么就会出现XMLHttpRequest cannot load. No 'Access ...
- 客官,您的 Flask 全家桶请收好
http://www.factj.com/archives/543.html Flask-AppBuilder - Simple and rapid Application buil ...
- flask_login 整合 pyjwt + json 简易flask框架
现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...
- 跨域资源共享 CORS 详解(转)
add by zhj: 公司在一个web产品上,做前后端分离,前后端提供独立的服务,使用不同的域名,通过http进行交互,在 前端,会涉及到跨域访问的问题,前端使用了CORS,后端同时需要做相应修改, ...
随机推荐
- JS图片懒加载
简介 当页面图片太多时,加载速度就会很慢.尤其是用2G/3G/4G访问页面,不仅页面慢,而且还会用掉很多流量.图片懒加载的原理就是将页面内所有需要加载的图片全部换成一张默认的图片(一般尺寸很小),只有 ...
- memcache和redis区别
memcache官方定义 Free & open source, high-performance, distributed memory object caching system, gen ...
- Atitit机器学习原理与概论book attilax总结
Atitit机器学习原理与概论book attilax总结 <机器学习(决战大数据时代!IT技术人员不得不读!)>((美)米歇尔(Mitchell)[简介_书评_在线阅读] -1 < ...
- 为什么获取的System.Web.HttpContext.Current值为null,HttpContext对象为null时如何获取程序(站点)的根目录
ASP.NET提供了静态属性System.Web.HttpContext.Current,因此获取HttpContext对象就非常方便了.也正是因为这个原因,所以我们经常能见到直接访问System.W ...
- vue for 绑定事件
vue for 绑定事件 <div id="pro_list" v-for="item in pro_list"> <div class=&q ...
- G2 DT时代的图形语法 正式发布
G2有一个高大上的名字叫做:The Grammar Of Graphics——图形语法.它是一个强大的语义化图表生成工具,它提供了一整套图形语法,可以让用户通过简单的语法搭建出无数种图表,并且集成了大 ...
- Visual-Studio-2015-Cheat-Sheet Visual Studio 2015 快捷键列表
PDF 文件下载 http://files.cnblogs.com/files/JamesLi2015/Visual-Studio-2015-Cheat-Sheet.pdf
- KlayGE 4.4中渲染的改进(一):只需要SM3的TBDR
转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2736 KlayGE从4.0开始引入deferred rendering层(DR),并且这几个 ...
- WPF Bitmap转Imagesource
var imgsource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),IntPtr ...
- java中并不是任意多个接口都可以实现多实现
interface A{ public abstract void show(); } interface B{ public abstract int show(); } public class ...