CORS实现跨域Ajax
客户端
#!/usr/bin/env python
import tornado.ioloop
import tornado.web class MainHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.render('index.html')
def post(self, *args, **kwargs):
self.render('index.html') class CorsHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.render('index.html')
def post(self, *args, **kwargs):
self.render('index.html')
settings ={
'template_path':'views',#html文件模板路径配置
'static_path':'statics',#css,js文件路径配置
'static_url_prefix':'/sss/',
}
application = tornado.web.Application([
(r"/index", MainHandler),
(r"/cors", CorsHandler),
],**settings)
if __name__ == "__main__":
application.listen(8000)
tornado.ioloop.IOLoop.instance().start()
app.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="button" value="Jsonp" onclick="JsonpAjax();">
<input type="button" value="DOJsonp" onclick="DoJsonpAjax();">
<script src="{{static_url('jquery-3.1.1.js')}}"></script>
<script>
/
function DoJsonpAjax() {
$.ajax({
url:'http://tao.com:8001/cors',
type:'POST',
data:{'k1':'v1'},
success:function (arg) {
console.log(arg)
}
})
}
function JsonpAjax() {
$.ajax({
url:'http://tao.com:8001/cors',
type:'PUT',
headers:{'h1':'HH1'},
data:{'k1':'v1'},
xhrFields:{withCredentials:true},
success:function (arg) {
console.log(arg)
}
})
}
// function DoJsonpAjax() {
//// var tag = document.createElement('script');
//// tag.src = "http://www.jxntv.cn/data/jmd-jxtv2.html?callback=list";
//// document.head.appendChild(tag);
////
//// document.head.removeChild(tag);
//////
// $.ajax({
// url: "http://tao.com:8001/index",
// type:"POST",
// dataType: 'jsonp',
// jsonp:'callback',//相当于在url: "http://tao.com:8001/index?callback= xxoo",
// jsonpCallback: "xxoo"//相当于在url: "http://tao.com:8001/index?callback= xxoo",
// })
// }
</script>
</body>
</html>
index.html
服务端
#!/usr/bin/env python
import tornado.ioloop
import tornado.web class MainHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
callback = self.get_argument('callback')
self.write("{}([11,22,33])".format(callback))
def post(self, *args, **kwargs):
self.write('t2.post')
class CorsHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.write('index.get')
def post(self, *args, **kwargs):
self.set_header("Access-Control-Allow-Origin","http://taochen.com:8000")
self.write('index.post')
def options(self, *args, **kwargs):
self.set_header("Access-Control-Allow-Origin", "http://taochen.com:8000")
self.set_header("Access-Control-Allow-Methods", "PUT,DELETE")
self.set_header("Access-Control-Allow-Headers", "h1")
self.set_header("Access-Control-Allow-Credentials",'true')
def put(self, *args, **kwargs):
print(self.cookies)
self.set_cookie('kk','kkk2')
self.set_header("Access-Control-Allow-Origin", "http://taochen.com:8000")
self.set_header("Access-Control-Allow-Credentials", 'true')
self.write("ok")
settings ={
'template_path':'views',#html文件模板路径配置
'static_path':'statics',#css,js文件路径配置
'static_url_prefix':'/sss/',
}
application = tornado.web.Application([
(r"/index", MainHandler),
(r"/cors", CorsHandler),
],**settings)
if __name__ == "__main__":
application.listen(8001)
tornado.ioloop.IOLoop.instance().start()
app.py
CORS实现跨域Ajax的更多相关文章
- 跨域资源共享(CORS)--跨域ajax
几年前,网站开发者都因为ajax的同源策略而撞了南墙.当我们惊叹于XMLHttpRequest对象跨浏览器支持所带来的巨大进步时,我们很快发现没有一个方法可以使我们用JavaScript实现请求跨域访 ...
- 第二百七十四节,同源策略和跨域Ajax
同源策略和跨域Ajax 什么是同源策略 尽管浏览器的安全措施多种多样,但是要想黑掉一个Web应用,只要在浏览器的多种安全措施中找到某种措施的一个漏洞或者绕过一种安全措施的方法即可.浏览器的各种保安措 ...
- 跨域Ajax -- jsonp和cors
跨域Ajax - jsonp - cors 参考博客: http://www.cnblogs.com/wupeiqi/articles/5703697.html http://www.cnblogs. ...
- [CORS:跨域资源共享] 同源策略与JSONP
Web API普遍采用面向资源的REST架构,将浏览器最终执行上下文的JavaScript应用Web API消费者的重要组成部分."同源策略"限制了JavaScript的跨站点调用 ...
- HTML5安全:CORS(跨域资源共享)简介。。。ie67不要想了。。。
来源:http://blog.csdn.net/hfahe/article/details/7730944 前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Go ...
- Angular通过CORS实现跨域方案
以前有一篇很老的文章网上转了很多,包括现在如果你百度"跨域"这个关键字,前几个推荐的都是"Javascript跨域总结与解决方案".看了一下感觉手段有点陈旧了, ...
- CORS(跨域资源共享)简介
前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...
- HTML5安全:CORS(跨域资源共享)简介
前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...
- TML5安全:CORS(跨域资源共享)简介
来源:http://blog.csdn.net/hfahe/article/details/7730944 前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Go ...
随机推荐
- java:system根据输入的内容,然后输出(字节流)
把输入的内容输出来:根据system.in的内容System.out.println输出出来 都是字节流,的形式: //限制读取的字符长度 //字节流 InputStream ips = System ...
- ES6-let命令
let命令 用于声明变量,但是声明的变量只能在let命令所在的代码块内有效, { let a = 10; var b = 1; } 其中,a在代码块的外部是调用不到的.对于for循环的计数器里面,就很 ...
- hive_异常_01_ Terminal initialization failed; falling back to unsupported
一.异常现象 hive初始化数据库时,在执行了 schematool -initSchema -dbType mysql 这个命令时,终端抛出如下异常: [ray@rayner bin]$ schem ...
- metaclass 了解一下
创建类的两种方式 方式一: class Foo(object,metaclass=type): CITY = "bj" def func(self,x): return x + 1 ...
- PhotoShop使用指南(3)—— 将多张图片添加到图层
第一步:选择文件菜单>脚本>将文件载入堆栈 第二步:点击浏览添加要批量载入的图片
- [独孤九剑]Oracle知识点梳理(八)常见Exception
本系列链接导航: [独孤九剑]Oracle知识点梳理(一)表空间.用户 [独孤九剑]Oracle知识点梳理(二)数据库的连接 [独孤九剑]Oracle知识点梳理(三)导入.导出 [独孤九剑]Oracl ...
- 数字排列(n,m)(搜索与回溯)
题目描述: 设有n个整数的集合{1,2,…,n},从中取出任意r个数进行排列(r<n),试列出所有的排列. 代码如下: #include<iostream>#include<c ...
- FPGA中竞争冒险问题的研究
什么是竞争冒险? 1 引言 现场可编程门阵列(FPGA)在结构上由逻辑功能块排列为阵列,并由可编程的内部连线连接这些功能块,来实现一定的逻辑功能. FPGA可以替代其他PLD或者各种中小规模数 ...
- Java 字符串和时间互相转化 +时间戳
一:字符串转换成date String datatime="2015-09-22 15:16:48"; SimpleDateFormat form = new SimpleDate ...
- Day2-Python基础2---集合和文件操作
一.集合操作 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 s = set([1,4,5,7,3, ...