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 函数覆盖的问题
今天遇到奇怪问题是,一个html里面引入一个新的js进来后,原来的按钮点击后(假设触发onclick函数)在某个地方卡住了,不往下执行了.—— 之前都是好好的. 调试发现,在onclick中间某一处调 ...
- Java生成验证码原理(jsp)
验证码的作用: 验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全自动区分计算机和人类的 ...
- Redis批量删除KEY的方法
Redis 中有删除单个 Key 的指令 DEL,但好像没有批量删除 Key 的指令,不过我们可以借助 Linux 的 xargs 指令来完成这个动作. 代码如下: redis-cli keys “* ...
- MVVM架构~knockoutjs系列之验证信息自定义输出~再续
返回目录 对于一个项目的表单验证,方式有很多,效果也有很多,具体使用哪种完成取决于产品这边,产品让你用什么,你就要用什么,而做为开发人员,我们要做的就是"整理近可能多的架构方式",这样才可以自由的应变 ...
- Atitit dsl exer v3 qb3 新特性
Atitit dsl exer v3 qb3 新特性 /atiplat_cms/src/com/attilax/dsl/DslParser.java V3 支持typeed参数,与简化的notyp参数 ...
- Linux初学 - 文件夹及文件操作
创建文件夹 mkdir 移动文件夹 mv dir1 dir2 复制文件夹 cp 删除文件夹 rm 创建文件 touch 编辑文件内容 vi /vim 查看文件内容 cat 追加文件内容 echo 复制 ...
- WinRAR注册
新建一个txt文件并命名为"rarreg.key",添加以下内容保存,然后放置在WinRAR安装目录: RAR registration data Federal Agency f ...
- C#多线程之旅(3)——线程池
v博客前言 先交代下背景,写<C#多线程之旅>这个系列文章主要是因为以下几个原因:1.多线程在C/S和B/S架构中用得是非常多的;2.而且多线程的使用是非常复杂的,如果没有用好,容易造成很 ...
- Ajax基础知识《一》
对于网站开发人员,一定不会陌生的Ajax技术,本篇就让我们认识一下它,或许在日后的开发过程中我们就可以使用到.Ajax在那方面使用的比较多呢?答案:表单注册,传统的表单注册,有时需要填写大量的信息,当 ...
- AngularJS的学习--ng-show/ng-hide/ng-if和ng-switch
在Angular的原生指令中有这几个指令用来控制元素的展示与否,ng-show/ng-hide/ng-if和ng-switch. 在angular性能优化中,我们也常常会用到它. 我们看下他们的区别. ...