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,后端同时需要做相应修改, ...
随机推荐
- CentOS Linux系统下安装Redis过程和配置参数说明
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/102.html?1455869303 安装过程: 代码如下: wget h ...
- 缓存篇(Cache)~第三回 HttpModule实现网页的文件级缓存
返回目录 再写完缓存篇第一回之后,得到了很多朋友的好评和来信,所以,决定加快步伐,尽快把剩下的文章写完,本篇是第三回,主要介绍使用HttpModule实现的文件级缓存,在看本文之前,大家需要限度Htt ...
- atitit。wondows 右键菜单的管理与位置存储
atitit.wondows 右键菜单的管理与位置存储 原理 .这样的功能称为Windows外壳扩展(Shell Extensions) 1 常用右键菜单 atiContentMenu1 通用tool ...
- linux 学习 设置固定网Ip
本人使用CentOs6.5 最近在学习linux操作系统,单在使用shell连接前都要使用ifconfig eth0 设置一个临时IP让我不胜其烦.决定学习设置一个固定IP 步骤: 1.登录计算机后使 ...
- jquery 插件开发
一.$.extend() 这种方式用来定义一些辅助方法是比较方便的 $.extend({ sayHello:function(name){ console.log('Hello:'+name); } ...
- Android Activity 常用技巧
1.设置 Activity 背景色为透明 在style.xml里面声明: <style name="TranslucentActivityStyle" parent=&quo ...
- 神马是 NaN,它的类型是神马?怎么测试一个值是否等于 NaN?
NaN 是 Not a Number 的缩写,JavaScript 的一种特殊数值,其类型是 Number,可以通过 isNaN(param) 来判断一个值是否是 NaN: console.log(i ...
- 浏览器端获取局域网IP地址,本机的MAC,以及机器名
原文链接:http://www.orlion.ga/59/ 只针对IE且客户端的IE允许AcitiveX运行 code: <html> <head> <title> ...
- java匿名类和匿名对象及this的其他用法
/* 匿名内部类:就是内部类的简写格式. 必须前提:内部类必须继承或者实现一个类或者接口. 匿名内部类其实就是一个匿名 子类对象. 格式:new 父类对象 or 接口(){ 子类内容:(覆盖父类的, ...
- Java多线程系列--“JUC原子类”02之 AtomicLong原子类
概要 AtomicInteger, AtomicLong和AtomicBoolean这3个基本类型的原子类的原理和用法相似.本章以AtomicLong对基本类型的原子类进行介绍.内容包括:Atomic ...