跨域API

简单跨域请求

只需要简单的设置允许跨域就可以了

def set_default_headers(self):
self.set_header('Access-Control-Allow-Origin', '*')

满足下面条件的就是简单请求,否则就不是

Simple requests

A simple cross-site request is one that meets all the following conditions:

The only allowed methods are:
GET
HEAD
POST
Apart from the headers set automatically by the user agent (e.g. Connection, User-Agent, etc.), the only headers which are allowed to be manually set are:
Accept
Accept-Language
Content-Language
Content-Type
The only allowed values for the Content-Type header are:
application/x-www-form-urlencoded
multipart/form-data
text/plain

复杂跨域请求

所有的非简单请求,比如content-Type:application/json的POST请求,CORS详解

复杂的跨域请求,浏览器会先发起一个OPTIONS类型的验证请求,检查服务端是否允许即将发起的真正的请求类型(主要是Methods和headers),如果允许的话,就需要像下面这样设置设置对应的rul

def set_default_headers(self):
self.set_header('Access-Control-Allow-Origin', '*')
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
self.set_header('Access-Control-Max-Age', 86400) #24 hours
self.set_header('Access-Control-Allow-Headers', '*')
#self.set_header('Content-type', 'application/json')

服务端可以对不同的url配置不同的跨域信息,这也是网站安全的基本配置

复杂请求的过程

发起的OPTIONS请求:

OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type

服务端对OPTIONS请求的响应:

HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:39 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Headers: X-PINGOTHER, Content-Type
Access-Control-Max-Age: 86400
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 0
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/plain

验证成功后发起真正的请求:

POST /resources/post-here/ HTTP/1.1
Host: bar.other
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
X-PINGOTHER: pingpong
Content-Type: text/xml; charset=UTF-8
Referer: http://foo.example/examples/preflightInvocation.html
Content-Length: 55
Origin: http://foo.example
Pragma: no-cache
Cache-Control: no-cache <?xml version="1.0"?><person><name>Arun</name></person> HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 01:15:40 GMT
Server: Apache/2.0.61 (Unix)
Access-Control-Allow-Origin: http://foo.example
Vary: Accept-Encoding, Origin
Content-Encoding: gzip
Content-Length: 235
Keep-Alive: timeout=2, max=99
Connection: Keep-Alive
Content-Type: text/plain [Some GZIP'd payload]

参考

http://www.oschina.net/question/1014827_115277?sort=time

http://blog.csdn.net/wangjun5159/article/details/49096445

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

跨域API的更多相关文章

  1. MVC跨域API

    API using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Ne ...

  2. c# 跨域api

    前端 ajax get请求 $.ajax({ url: "API地址", type: 'get', dataType: 'jsonp', async: true, processD ...

  3. php跨域请求

    跨域api服务器设置 header('content-type:application:json;charset=utf8'); header('Access-Control-Allow-Origin ...

  4. Ajax跨域问题

    为了数据安全,默认情况下ajax请求是不支持跨域访问. 但是可以通过以下方法让ajax接收跨域API地址 第一种:JSONP比较老,较简单的请求可以考虑使用. JS代码 $(function () { ...

  5. 【Vue+Node】解决axois请求数据跨域问题

    项目基于Vue前端+Node后台,启动两个服务,请求数据时,端口不一致造成跨域报错: (No 'Access-Control-Allow-Origin' header is present on th ...

  6. Vue使用vue-recoure + http-proxy-middleware + vuex配合promise实现基本的跨域请求封装

    使用vue init webpack 你的项目名称初始化一个vue的项目 安装依赖 npm install vue-resource http-proxy-middleware vuex koa 在项 ...

  7. 原创:【ajax | axios跨域简单请求+复杂请求】自定义header头Token请求Laravel5后台【亲测可用】

    如标题:我想在ajax的header头增加自定义Token进行跨域api认证并调用,api使用laravel5编写,如何实现? 首先,了解下CORS简单请求和复杂请求.  -- CORS简单请求 -- ...

  8. springboot+vue2.x 解决session跨域失效问题

    服务端SpringBoot2.x   :localhost:8082 前端Vue2.x                 :localhost:81 前后端的端口号不同,为跨域,导致前端访问后端时,每次 ...

  9. uni-app运行到浏览器跨域H5页面的跨域问题解决方案

    官方文档对跨域的解决方案推荐: https://ask.dcloud.net.cn/article/35267 更方便的解决方案 项目根目录直接创建一个vue.config.js文件,并在里面配置代理 ...

随机推荐

  1. 高等微积分及其应用 nicholas.pdf——下载地址

    http://pan.baidu.com/share/link?shareid=426414429&uk=3977204074 这本书在网络上很少有了,本人是在群里下过来整理成一个文档了,由n ...

  2. <Oracle Database>后台进程

    进程监视器进程(PMON)  这个进程负责在出现异常中止的连接之后完成清理.PMON会回滚未提交的工作,并释放为失败进程分配的SGA资源.PMON还负责监视其他的Oracle后台进程,并在必要时(如果 ...

  3. uva 11806 Cheerleaders

    // uva 11806 Cheerleaders // // 题目大意: // // 给你n * m的矩形格子,要求放k个相同的石子,使得矩形的第一行 // 第一列,最后一行,最后一列都必须有石子. ...

  4. sharepoint学习。

    企业门户:对全公司共用的信息进行统一管理.存储和发布,确保信息在公司范围内能够被及时传递 报表中心:集中管理.授权并发布所有业务报表,为各级管理人员提供各种数据.图形分析报表 办公协作:提供用户日常工 ...

  5. cordova for ios(android一样)添加插件

    1.进入当前工程文件夹 终端:cd ~/Desktop/ cd piao 2.添加插件 :cordova plugin add Basic device information (Device API ...

  6. 基于AutoCAD的ObjectARX之NET扩展(mcnetarx)-AcdbEntMake

    1.创建一个结果缓冲区. 2.调用AcdbEntMake创建对象. 示例: ' 创建文字实体 Dim rb As ResultBuffer = New ResultBuffer rb.Add(New ...

  7. centos 安装apache 和 php5.6

    centos 安装 php5.6,安装前需要先安装数据库和apache 安装 apache2.4 安装基础依赖:# yum install gcc gcc-c++ glibc glibc-devel ...

  8. Fragment、Activity 保存状态

    Activity 保存状态1. void onCreate(Bundle savedInstanceState) 当Activity被第首次加载时执行.我们新启动一个程序的时候其主窗体的onCreat ...

  9. Hibernate自动创建表

    只要在hibernate.cfg.xml添加这句话,就可以自动生成数据表 <property name="hibernate.hbm2ddl.auto">update& ...

  10. Xamarin.Forms.Platform.Perspex, Xamarin Forms 的 Perspex(号称下一代WPF) 实现

    Perspex, 跨平台的UI框架,加上Xamarin Forms的跨平台的中间层,这样同一套代码就可跨几乎所有已知平台,这其中包括旧版Windows, Linux及Mac OS. 目前,基本控件可显 ...