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. Atitit 破解qq空间(2)-------探测权限

    Atitit 破解qq空间(2)-------探测权限 /AtiPlatf_cms/src/com/attilax/net/httpTest.java  package com.attilax.net ...

  2. qqzoneQQ空间漏洞扫描器的设计attilax总结

    qqzoneQQ空间漏洞扫描器的设计attilax总结 1.1. 获取对方qq(第三方,以及其他机制)1 1.2. QQ空间的html流程1 1.3. 判断是否有权限1 1.4. 2015年度Web服 ...

  3. poi 输出Excel显示内容

    在业务系统中多少回接触到Excel解析.在java开发平台下选择 Apache POI是一个非常明智的选择,POI提供非常完善API来读取或写入Microsoft Office Excel. 目前对导 ...

  4. 静态(static)代码块、构造代码块、构造函数、父类子类执行顺序

    静态代码块:static修饰的代码块. 在类加载-初始化的时候进行,主要目的是给变量赋予初始值 构造代码块:直接在类中定义且没有加static关键字的代码块称为构造代码块. java会把构造代码块放到 ...

  5. 快速入门系列--WebAPI--01基础

    ASP.NET MVC和WebAPI已经是.NET Web部分的主流,刚开始时两个公用同一个管道,之后为了更加的轻量化(WebAPI是对WCF Restful的轻量化),WebAPI使用了新的管道,因 ...

  6. javase基础复习攻略《五》

    总结完JAVA的基础语法和面向对象思想后,今天为大家补充一下JAVA中的数组,数组是什么呢?大家是不是想到了集合,数组和集合有相似之处,集合中的数据无序,不可以重复.数组中则存放着具有相同特征的一组数 ...

  7. [转载]AxureRP常用快捷键

    习惯用Axure快捷键会让你做原型的时候更得心应手.Axure中文网总结了常用的一些快捷键分享给大家 . Axure RP Pro 6.5快捷键大全,如有疏漏,欢迎补充.   基本快捷键:   打开: ...

  8. 开发人员看测试之细说JBehave

    上篇我们说到如何从Github上clone出一个JBehave项目,既是为了学习JBehava,也是为了熟悉下Github.从clone下来的项目看来,基本没什么问题,稍微捋一捋就可以运行,但是就cl ...

  9. [Python] Ubuntu12.04LTS

    Ubuntu 12.04LTS中缺省安装了Python2.7.3. python -h 查看可用选项 python -V 查看Python版本 下面写个简单的测试程序: 新建HelloWorld.py ...

  10. C#日期格式转换

    DateTime dt = DateTime.Now; // Label1.Text = dt.ToString();//2005-11-5 13:21:25 // Label2.Text = dt. ...