1、cors 是什么
1. 随着技术的发展,现在的浏览器可以支持主动设置从而允许跨域请求,即:跨域资源共享(CORS,Cross-Origin Resource Sharing)
2. 其本质是设置响应头,使得浏览器允许跨域请求。
2、简单请求必须满足的两个条件(不满足就是 复杂请求)
1. 条件 1: 请求方式:HEAD、GET、POST
2. 条件 2: 请求头信息:
Accept
Accept-Language
Content-Language
Last-Event-ID
Content-Type 对应的值是以下三个中的任意一个
application/x-www-form-urlencoded
multipart/form-data
text/plain
3、简单请求和非简单请求的区别
简单请求 :一次请求
非简单请求 :两次请求,在发送数据之前会先发一次请求用于做 "预检",只有 "预检" 通过后才再发送一次请求用于数据传输
4、关于 "预检"
1. 请求方式:OPTIONS
2. "预检" 其实做检查,检查如果通过则允许传输数据,检查不通过则不再发送真正想要发送的消息
3. 如何 "预检"
1) 如果复杂请求是 PUT 等请求,则服务端需要设置允许某请求,否则 "预检" 不通过
Access-Control-Request-Method
2) 如果复杂请求设置了请求头,则服务端需要设置允许某请求头,否则 "预检" 不通过
Access-Control-Request-Headers
1、说明
1. 由于复杂请求时,首先会发送 "预检" 请求,如果 "预检" 成功,则发送真实数据。
2. "预检" 请求时,允许请求方式则需服务器设置响应头:Access-Control-Request-Method
3. "预检" 请求时,允许请求头则需服务器设置响应头:Access-Control-Request-Headers
4. "预检" 缓存时间,服务器设置响应头:Access-Control-Max-Age
2、tornado 测试 cors 步骤
1. 创建两个 tornado 项目: tom_tornado(客户端域)、jack_tornado(服务端域)
2、修改 C:\Windows\System32\drivers\etc 路径下的 hosts 文件,添加两条 hosts 记录
127.0.0.1 tom.com
127.0.0.1 jack.com
3、在 http://tom.com:8000/get_date 的 get_date.html 文件通过 ajax 向 http://jack.com:8888/index 获取数据
4、创建 tom_tornado 项目
- import tornado.ioloop
- import tornado.web
- class MainHandler(tornado.web.RequestHandler):
- def get(self):
- self.set_header('Access-Control-Allow-Origin', "")
- self.render('get_data.html')
- settings = {
- 'template_path': 'template',
- 'static_path': 'static',
- 'static_url_prefix': '/static/',
- }
- application = tornado.web.Application([
- (r"/get_date", MainHandler),
- ], **settings)
- if __name__ == "__main__":
- application.listen(8000)
- print('http://tom.com:8000/get_date')
- tornado.ioloop.IOLoop.instance().start()
- lang="en">
- charset="UTF-8">
- type="submit" onclick="XmlSendRequest();" />
- type="submit" onclick="JqSendRequest();" />
- type="text/javascript" src="/static/jquery-1.12.4.js">
- function XmlSendRequest() {
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- var result = xhr.responseText;
- console.log(result);
- }
- };
- xhr.open('GET', "http://jack.com:8888/index", true);
- xhr.send();
- }
- function JqSendRequest() {
- $.ajax({
- url: "http://jack.com:8888/index",
- type: 'GET',
- dataType: 'text',
- success: function(data, statusText, xmlHttpRequest) {
- console.log(data);
- }
- })
- }
5. 创建 jack_tornado 项目
- import tornado.ioloop
- import tornado.web
- import json
- class IndexHandler(tornado.web.RequestHandler):
- def get(self):
- self.set_header("Access-Control-Allow-Origin", "*")
- self.set_header("Access-Control-Allow-Headers", "x-requested-with") # 允许请求头则需服务器设置响应头
- self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') # 允许请求方式则需服务器设置响应头
- # self.set_header('Access-Control-Max-Age', 10) # "预检"缓存时间,服务器设置响应头
- self.write('{"status": true, "data": "seven"}')
- settings = {
- 'template_path': 'views',
- 'static_path': 'static',
- 'static_url_prefix': '/static/',
- }
- application = tornado.web.Application([
- (r"/index", IndexHandler),
- ], **settings)
- if __name__ == "__main__":
- application.listen(8888)
- print('http://jack.com:8888/index')
- tornado.ioloop.IOLoop.instance().start()
来源: http://www.bubuko.com/infodetail-2428610.html