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的更多相关文章

  1. flask跨域问题

    在Flask开发RESTful后端时,前端请求会遇到跨域的问题.下面是解决方法: 使用 flask-cors库可以很容易的解决   1 pip install flask-cors 两种方法,一个是全 ...

  2. 七月小说网 Python + GraphQL (三)

    概述 后台数据库几个基本表基本搭建完毕,看了下Github Develop的V4 Api抛弃了RESTful,采用GraphQL,感觉很有意思,一看文档,竟然有Python的开源实现 Graphene ...

  3. tornado django flask 跨域解决办法(cors)

    XMLHttpRequest cannot load http://www.baidu.com. No 'Access-Control-Allow-Origin' header is present ...

  4. 使用CORS解决flask前端页面跨域问题

    from flask import Flask from flask_cors import CORS app = Flask(__name__) CORS(app) @app.route(" ...

  5. Flask配置Cors跨域

    1 跨域的理解 跨域是指:浏览器A从服务器B获取的静态资源,包括Html.Css.Js,然后在Js中通过Ajax访问C服务器的静态资源或请求.即:浏览器A从B服务器拿的资源,资源中想访问服务器C的资源 ...

  6. flask之CORS跨域请求处理

    from flask import Flask from flask_cors import CORS#pip install Flask-CORS#跨域请求模块 app = Flask(__name ...

  7. angularjs flask跨域问题 XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin'

    场景,我要来我的server(A)上用api来访问另一个server(B)的问题,如果直接在A上调用B的api,那么就会出现XMLHttpRequest cannot load. No 'Access ...

  8. 客官,您的 Flask 全家桶请收好

    http://www.factj.com/archives/543.html Flask-AppBuilder          - Simple and rapid Application buil ...

  9. flask_login 整合 pyjwt + json 简易flask框架

    现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...

  10. 跨域资源共享 CORS 详解(转)

    add by zhj: 公司在一个web产品上,做前后端分离,前后端提供独立的服务,使用不同的域名,通过http进行交互,在 前端,会涉及到跨域访问的问题,前端使用了CORS,后端同时需要做相应修改, ...

随机推荐

  1. 谷歌大牛 Rob Pike 的 5 个编程原则

    谷歌大牛 Rob Pike 的 5 个编程原则 简介: Rob Pike,目前谷歌公司最著名的软件工程师之一,曾是贝尔实验室Unix开发团队成员,Plan9操作系统开发的主要领导人,Inferno操作 ...

  2. Atiit 如何手写词法解析器

    Atiit 如何手写词法解析器 1.1. 通过编程直接从正则->nfa->dfa->表驱动词法解析一条龙自动生成.那是用程序自动生成是需要这样的,自己手写完全不必要这么复杂1 1.2 ...

  3. Atitit.现在的常用gui技术与gui技术趋势评价总结

    Atitit.现在的常用gui技术与gui技术趋势评价总结 1. Gui俩种分类:  native 和 dsl 和 script1 2. 最好的跨平台gui技术h51 2.1. 几大技术体系(java ...

  4. BugHD for JavaScript上线,轻松收集前端 Error

    从收集 APP 崩溃信息到全面收集网站出现的 Error,现在的 BugHD 变得更加强大.目前,BugHD JS Error 收集功能 已正式上线,前端 er 们不用再面对一堆 Bug 无处下手. ...

  5. C#设计模式-单例模式

    单例模式三种写法: 第一种最简单,但没有考虑线程安全,在多线程时可能会出问题…… public class Singleton { private static Singleton _instance ...

  6. Python的枚举类型

    Python的 Python的没有我们有两种用法: 创建Enum的实例 创建Enum的subclass 创建Enum的实例 from enum import Enum, unique Month = ...

  7. 大型.NET商业软件代码保护技术 技术与实践相结合保护辛苦创造的劳动成果

    列举工作以来遇到的各种类型的软件所采用的代码保护技术,只讲原理不涉及技术细节实现,以避免产生法律问题.有些朋友说直接把代码放在Github开源下载,开源可以促进技术交流与进步,然而值钱的代码都积压在硬 ...

  8. java持久层框架mybatis如何防止sql注入

    看到一篇很好的文章:http://www.jfox.info/ava-persistence-framework-mybatis-how-to-prevent-sql-injection sql注入大 ...

  9. poj3342Party at Hali-Bula(树形dp)

    /* 树形dp! 判重思路: 当dp[v][0]==dp[v][1]时,很自然,flag[u][0]必然是有两种方案的.flag[u][1]则不然, 因为它只和dp[v][0]有关系.而若flag[v ...

  10. 拓扑排序(二)之 C++详解

    本章是通过C++实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处:http://www.cnblogs. ...