跨域API
跨域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的更多相关文章
- MVC跨域API
API using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Ne ...
- c# 跨域api
前端 ajax get请求 $.ajax({ url: "API地址", type: 'get', dataType: 'jsonp', async: true, processD ...
- php跨域请求
跨域api服务器设置 header('content-type:application:json;charset=utf8'); header('Access-Control-Allow-Origin ...
- Ajax跨域问题
为了数据安全,默认情况下ajax请求是不支持跨域访问. 但是可以通过以下方法让ajax接收跨域API地址 第一种:JSONP比较老,较简单的请求可以考虑使用. JS代码 $(function () { ...
- 【Vue+Node】解决axois请求数据跨域问题
项目基于Vue前端+Node后台,启动两个服务,请求数据时,端口不一致造成跨域报错: (No 'Access-Control-Allow-Origin' header is present on th ...
- Vue使用vue-recoure + http-proxy-middleware + vuex配合promise实现基本的跨域请求封装
使用vue init webpack 你的项目名称初始化一个vue的项目 安装依赖 npm install vue-resource http-proxy-middleware vuex koa 在项 ...
- 原创:【ajax | axios跨域简单请求+复杂请求】自定义header头Token请求Laravel5后台【亲测可用】
如标题:我想在ajax的header头增加自定义Token进行跨域api认证并调用,api使用laravel5编写,如何实现? 首先,了解下CORS简单请求和复杂请求. -- CORS简单请求 -- ...
- springboot+vue2.x 解决session跨域失效问题
服务端SpringBoot2.x :localhost:8082 前端Vue2.x :localhost:81 前后端的端口号不同,为跨域,导致前端访问后端时,每次 ...
- uni-app运行到浏览器跨域H5页面的跨域问题解决方案
官方文档对跨域的解决方案推荐: https://ask.dcloud.net.cn/article/35267 更方便的解决方案 项目根目录直接创建一个vue.config.js文件,并在里面配置代理 ...
随机推荐
- Fragment要点复习
做系统时间久了,应用层的很多东西都忘了,但是基础还是得巩固,下面对fragment相关的知识点简单整理一下. 一.fragment的引入 fragment是从Android3.0(API level ...
- 我对C#的理解
C#里面所有东西都可以看作对象.接口,类,枚举等等. 类是最常用的,可以继承别的接口,类等,就会自动拥有别人的功能. 接口是类的概要.给别人看的协议,好像一个人对外做出的承诺. 抽象类是实现了部分承诺 ...
- JDK6环境下升级项目到springframework4.x和tomcat7.x
springframework 3.x升级到 4.x 1 xsi:schemaLocation 对应的3.x->4.x 2 pom springframework <propertie ...
- core的生成
ubuntu12.04,默认无法生成core文件 输入ulimit -c 1024就ok了 列一下ulimit的参数,貌似这个命令是系统性能控制相关的,先不管了. 表 1. ulimit 参数说明 选 ...
- Python成长笔记 - 基础篇 (七)python面向对象
三大特性: 1.封装:在类中对数据赋值.内部调用对外部用户是透明的,这使类变成了一个胶囊或容器,里面包含着类的数据和方法 2.继承:一个类可以派生出子类,在父类中定义的属性.方法会自动被子类继承 ...
- CUTE-WRV2.1+TCP core(COM5402)的TCP带宽测试
问题的提出 测试TCP的带宽,硬件平台CUTE-WRV2.1,固件版本WRPCV3.0+COM5402. 代码.工具及实现 1. 在固件中,增加模块,收到TCP数据包后不断地往外发数: library ...
- 【C-分支结构】
一.关系运算符 双目运算符 自左向右结合 关系运算符的优先级低于算术运算符,高于赋值运算符 <(小于) <=(小于或等于) >(大于) >=(大于或等于) ==(等于) !=( ...
- 持续集成(CI)相关的一些工具,后续补充。。。。
持续集成的目标:使项目开发更加便捷 1.make工具 最原始的工具,负责组织构建的过程,即指挥编译器如何编译,连接器如何链接,最后生成一个可用文件. 2.Ant工具 Ant是一个构建工具,它只有一个配 ...
- Java使用velocity导出word
效果展示: 使用word编辑好模板
- ORACLE中的DECODE函数
今天遇到一个问题,数据库中需要排序的字段是中文,但是有不能按照中文拼音来排序,在网上查到一些资料,完美的解决了问题. DECODE(DS.RANGE_NAME,'高',3,'中',2,'低',1,'潜 ...