客户端

#!/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的更多相关文章

  1. 跨域资源共享(CORS)--跨域ajax

    几年前,网站开发者都因为ajax的同源策略而撞了南墙.当我们惊叹于XMLHttpRequest对象跨浏览器支持所带来的巨大进步时,我们很快发现没有一个方法可以使我们用JavaScript实现请求跨域访 ...

  2. 第二百七十四节,同源策略和跨域Ajax

    同源策略和跨域Ajax 什么是同源策略  尽管浏览器的安全措施多种多样,但是要想黑掉一个Web应用,只要在浏览器的多种安全措施中找到某种措施的一个漏洞或者绕过一种安全措施的方法即可.浏览器的各种保安措 ...

  3. 跨域Ajax -- jsonp和cors

    跨域Ajax - jsonp - cors 参考博客: http://www.cnblogs.com/wupeiqi/articles/5703697.html http://www.cnblogs. ...

  4. [CORS:跨域资源共享] 同源策略与JSONP

    Web API普遍采用面向资源的REST架构,将浏览器最终执行上下文的JavaScript应用Web API消费者的重要组成部分."同源策略"限制了JavaScript的跨站点调用 ...

  5. HTML5安全:CORS(跨域资源共享)简介。。。ie67不要想了。。。

    来源:http://blog.csdn.net/hfahe/article/details/7730944 前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Go ...

  6. Angular通过CORS实现跨域方案

    以前有一篇很老的文章网上转了很多,包括现在如果你百度"跨域"这个关键字,前几个推荐的都是"Javascript跨域总结与解决方案".看了一下感觉手段有点陈旧了, ...

  7. CORS(跨域资源共享)简介

    前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...

  8. HTML5安全:CORS(跨域资源共享)简介

    前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...

  9. TML5安全:CORS(跨域资源共享)简介

    来源:http://blog.csdn.net/hfahe/article/details/7730944 前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Go ...

随机推荐

  1. 安装SQL 2008失败 (win7 旗舰版 32位)

    本机系统 win7 32位 旗舰版 机器已经有sql 2005了,2008 不能安装成功,而且无任何错误提示. 那么通过windows install clean up (下载 windows ins ...

  2. 为mac编写swift脚本

    代码示例: #!/usr/bin/env xcrun swift print("Hello World") 可以用Sublime Text编写,安装Swift包后有语法着色功能.然 ...

  3. Rainmeter如何打开控制面板的小程序

    控制面板功能都是通过访问cpl文件来关联它们的,假设你的系统盘在C盘,那么它们的本地在C:\Windows\System32\ Rainmeter通过使用这个应用程序C:\Windows\System ...

  4. Microsoft Visual Studio 2012 Update 4 RC 3 离线安装程序

    Microsoft Visual Studio 2012 Update 4 RC 3 离线安装程序 ☆ 微软官网地址:☆ http://www.microsoft.com/en-us/download ...

  5. VC6常用插件

    VC6常用插件 2009-10-09 17:27 1.Visual Assist(强烈推荐)    http://www.wholetomato.com/     VA从5.0一直到现在的VAX,功能 ...

  6. 欧拉函数 and 大数欧拉 (初步)

    前两天总结了素数筛法,其中就有Eular筛法.现在他又来了→→ φ(n),一般被称为欧拉函数.其定义为:小于n的正整数中与n互质的数的个数. 毕竟是伟大的数学家,所以以他名字命名的东西很多辣. 对于φ ...

  7. Java内存垃圾回收机制(转贴)

    Java的堆是一个运行时数据区,类的实例(对象)从中分配空间.Java虚拟机(JVM)的堆中储存着正在运行的应用程序所建立的所有对象,这些对象通 过new.newarray.anewarray和mul ...

  8. ACM学习历程—Hihocoder 1291 Building in Sandbox(dfs && 离线 && 并查集)

    http://hihocoder.com/problemset/problem/1291 前几天比较忙,这次来补一下微软笔试的最后一题,这题是这次微软笔试的第四题,过的人比较少,我当时在调试B题,没时 ...

  9. deque容器

    一.deque容器基本概念 deque是“double-ended queue”的缩写,和vector一样,deque也支持随机存取.vector是单向开口的连续性空间,deque则是一种双向开口的连 ...

  10. 基于JQ的简单左右轮播图

    // 轮播图 主要实现思想: a.第一层div,设置overflow为hidden. b.里面是一个ul,每个li里面有个img或者为每个li设置背景图片也可以. c.li设置为左浮动,排成一行,还有 ...