博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tornado 添加请求头进行允许跨域
阅读量:4322 次
发布时间:2019-06-06

本文共 2493 字,大约阅读时间需要 8 分钟。

什么是跨域?

这个例子是csdn找的, 声明下哈 什么是跨域?跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。所谓同源是指,域名,协议,端口均相同,不明白没关系,举个栗子:http://www.123.com/index.html 调用 http://www.123.com/server.php (非跨域)http://www.123.com/index.html 调用 http://www.456.com/server.php (主域名不同:123/456,跨域)http://abc.123.com/index.html 调用 http://def.123.com/server.php (子域名不同:abc/def,跨域)http://www.123.com:8080/index.html 调用 http://www.123.com:8081/server.php (端口不同:8080/8081,跨域)http://www.123.com/index.html 调用 https://www.123.com/server.php (协议不同:http/https,跨域)请注意:localhost和127.0.0.1虽然都指向本机,但也属于跨域。

 

 

 服务端进行跨域需要添加的代码:

class BaseHandler(tornado.web.RequestHandler):    def set_default_headers(self):        print("setting headers!!!")        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')    def post(self):        self.write('some post')    def get(self):        self.write('some get')    def options(self):        # no body        self.set_status(204)        self.finish()

 

 

可以按照前端目录ajax里面代码进行测试

js ---代码未测试

$.ajax({   url: "http://some_tornado/api",   type: "POST",   crossDomain: true,   data: 'some_data',   success: function (response) {     alert(response);   },   error: function (xhr, status) {     alert("error");   } });

 

     

HTTPServerRequest(    protocol='http',     host='127.0.0.1:8000',     method='POST',     uri='/index',     version='HTTP/1.1',     remote_ip='127.0.0.1',     headers={                'Host': '127.0.0.1:8000',                 'X-Requested-With': 'XMLHttpRequest',                 'Content-Length': '4977',                 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',                'Origin': 'http://127.0.0.1:8000',                'Referer': 'http://127.0.0.1:8000/index',                 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryvxqv8ydaMOJHVHin',                 'Accept': '*/*',                  'Accept-Encoding': 'gzip, deflate, br',                  'Connection': 'keep-alive',                  'Accept-Language': 'zh-CN,zh;q=0.8'}      ){
'file': [ {
'content_type': 'image/png', 'body': '', 'filename': '2C($GHY%V5J8WXAO2S_M8@4.png'}, {
'content_type': 'image/png', 'body': '', 'filename': '6CLT@ZF9(Y]76$3T(]3U0IY.png'} ]}

 

转载于:https://www.cnblogs.com/renfanzi/p/6952871.html

你可能感兴趣的文章
团队-团队编程项目爬取豆瓣电影top250-代码设计规范
查看>>
【linux驱动分析】之dm9000驱动分析(六):dm9000_init和dm9000_probe的实现
查看>>
json具体解释
查看>>
十一:Java之GUI图形Awt和Swing
查看>>
.net在arraylist用法
查看>>
android程序报错“error launching activity com.android.ddmlib.shellcommandunresponsiveexception”的解决方式...
查看>>
ORACLE中CONSTRAINT的四对属性
查看>>
DbVisualizer Pro 9.5.2中文乱码问题
查看>>
numpy.tile()
查看>>
[bzoj3944] Sum
查看>>
hadoop2.7节点的动态增加与删除
查看>>
Ogre: 天空
查看>>
关于NSDictionary的一点感悟
查看>>
CSS长度单位:px和pt的区别
查看>>
50.分治算法练习: 二分算法: 2703 奶牛代理商 XII
查看>>
Wampserver 403问题
查看>>
mysql日志详细解析
查看>>
解决关闭app权限弹框后无法识别页面对象问题
查看>>
struts2_对Map进行双层迭代
查看>>
asp.net是什么?asp.net、vb.net和c#.net有什么区别?
查看>>