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,后端同时需要做相应修改, ...
随机推荐
- Nodejs·网络服务
本章是从NodeJS拥有的模块角度,讲述了网络服务中的应用: net ----- > TCP dgram --> UDP http -----> HTTP https ----> ...
- SSM 三大框架整合
上一篇已经讲了整个各个子模块的创建过程以及它们之间的依存关系, 那么这一篇就来正式的整合三大框架(SSM)了. 1, 准备环境1.1 为每个War包工程创建一个Server 那么 添加了Server后 ...
- [数据库连接池二]Java数据库连接池--C3P0和JDNI.
前言:上一篇文章中讲了DBCP的用法以及实现原理, 这一篇再来说下C3P0和JDNI的用法. 1.1.C3P0数据源 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规 ...
- Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换
Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换 { "@type":"java.lang.Runti ...
- Atitit mtp ptp rndis midi协议的不同区别
Atitit mtp ptp rndis midi协议的不同区别 1. PTP:1 2. MTP:1 3. Mtp 与usb区别2 4. 不过和UMS相比,MTP也有不足之处:3 5. MTP协议介 ...
- iOS-Objective-C内存管理
内存管理: 栈区 [stack]:由编译器自动分配并释放,一般存放函数的参数值,局部变量等 堆区 [heap]:由程序员分配和释放,如果程序员不释放,程序结束时,可能会由操作系统回收 全局区(静态区) ...
- Java EE开发平台随手记4——Mybatis扩展3
接着昨天的Mybatis扩展——IDaoTemplate接口. 扩展9:批量执行 1.明确什么是批量执行 首先说明一下,这里的批量执行不是利用<foreach>标签生成一长串的sql字符串 ...
- PHP将富文本编辑后的内容,去除样式图片等只保留txt文本内容
1.从数据库读取富文本内容样式如下: <p style=";text-indent: 0;padding: 0;line-height: 26px"><span ...
- iOS苹果企业证书被撤销以及启用与管理
在国内, 积分墙以及各大助手(爱思助手, 91苹果助手, XY苹果助手, PP助手, 快用助手)等业务领域都在使用,苹果对证书的使用越来越严格.简单的分析一下,证书被封的原因. 一般证书被封会收到 ...
- JAVA--网络编程(UDP)
上午给大家简单介绍了一下TCP网络通信的知识,现在就为大家补充完整网络编程的知识,关于UDP的通信知识. UDP是一种不可靠的网络协议,那么还有什么使用价值或必要呢?其实不然,在有些情况下UDP协议可 ...