客户端

#!/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. linux(centos)安装Maven

    搭建maven 下载jar包,将apache-maven-3.2.2-bin.tar.gz上传到服务器 1.解压 tar -zvxf apache-maven-3.2.2-bin.tar.gz 2.移 ...

  2. php:PHP解析xml的4种方法

    XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML, XMLReader, DOMDocument. ...

  3. windows live writer 原始图片大小设置

          点击图片,右面对图片参数进行设置,然后点击保存默认设置. 那么以后再插入图片,就不要重新操作了.    

  4. 吐槽XE3中的BUG:无法调试32位的应用程序

    我想用的功能在XE5中有BUG, 无奈转移到XE3中测试,发现了XE3还有另外一个问题:无法DEBUG 32位的应用程序,这算什么事啊?有人说把项目属性中的link with dynamic RTL去 ...

  5. lucene搜索之facet查询原理和facet查询实例——TODO

    转自:http://www.lai18.com/content/7084969.html Facet说明 我们在浏览网站的时候,经常会遇到按某一类条件查询的情况,这种情况尤以电商网站最多,以天猫商城为 ...

  6. pandas 中文快速查询手册

    本文翻译自文章: Pandas Cheat Sheet - Python for Data Science ,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非 ...

  7. Git学习--版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  8. winform中的状态栏,以及在状态栏目上显示时间

    1:在winform上添加状态栏,并且在状态栏目上多添加几个label. step1:拖一个StatusStrip到winform上,名字默认为statusStrip1.找到statusStrip1的 ...

  9. python2和python3 print输出不换行

    python2 print不换行 在print最后加上一个逗号,会把两个输出打印在同一行,不过两个输出之间有一个空格的间隔,例如:print '{0}'.format(123),print '{0}' ...

  10. ACC 001 C - Shorten Diameter 图论

    题目: Problem Statement Given an undirected tree, let the distance between vertices \(u\) and \(v\) be ...