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. hammer.js手势库使用

    hammer.js是一款移动端手势库组件,支持pan(拖动).swipe(滑动).tap(轻触).press(按压,即长按).doubletap(双击)等很多手势操作,提供比较完善的事件监听机制,但是 ...

  2. 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序

    学习ASP.NET MVC系列: 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序 学习ASP.NET MVC(二)——我的第一个ASP.NET MVC 控制器 学习ASP ...

  3. 快速入门系列--WebAPI--04在老版本MVC4下的调整

    WebAPI是建立在MVC和WCF的基础上的,原来微软老是喜欢封装的很多,这次终于愿意将http编程模型的相关细节暴露给我们了.在之前的介绍中,基本上都基于.NET 4.5之后版本,其System.N ...

  4. Orlion个人博客 | 全栈工程师之路-www.orlion.ga

    自己用wordpress+bootstrap搭建了一个博客,网址http://www.orlion.ga,用来记录博主学习和成长,主要关注JAVA.LANMP.前端与客户端(Android).算法与数 ...

  5. 深入理解CSS计数器

    × 目录 [1]创建计数器 [2]使用计数器 [3]DEMO 前面的话 我们对计数器已经不陌生了,有序列表中的列表项标志就是计数器. 创建计数器 创建计数器的基础包括两个方面,一是能重置计数器的起点, ...

  6. Strophe.js连接XMPP服务器Openfire、Tigase实现Web私聊、群聊(MUC)

    XMPP(Extensible Messaging and Presence Protocol)是一种网络即时通讯协议,它基于XML,具有很强的扩展性,被广泛使用在即时通讯软件.网络游戏聊天.Web聊 ...

  7. Linux命令之diff

    1.命令格式: diff[参数][文件1或目录1][文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时才有效.以逐行的方式,比较文 ...

  8. 使用bokeh-scala进行数据可视化

    目录 前言 bokeh简介及胡扯 bokeh-scala基本代码 我的封装 总结 一.前言        最近在使用spark集群以及geotrellis框架(相关文章见http://www.cnbl ...

  9. select语句for update---转载

    作用: Select…For Update语句的语法与select语句相同,只是在select语句的后面加FOR UPDATE [NOWAIT]子句. 该语句用来锁定特定的行(如果有where子句,就 ...

  10. js基础篇——encodeURI 和encodeURIComponent

    转自zccst的又一次掉进encodeURIComponent的坑里了 问题: ajax.get ( url+'?k1'=v1+'&k2'=v2+'&k3'=v3, ... ); 由于 ...