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 ...
随机推荐
- 02-大鸭梨博客系统数据库设计及Dapper的使用
毫无疑问,数据库的设计在一个系统中起了至关重要的作用.我们都知道,系统设计分为两部分,或者说是两个阶段,即数据库设计和功能设计.构建一个完善的系统需要这两个阶段的充分考量.周密设计.合理联接以及密切配 ...
- 持续集成平台jenkins
一. 什么是持续集成 1. 概念 持续集成(Continuous Integration),也就是我们经常说的CI 持续集成(CI)是一种实践,可以让团队在持续的基础上收到反馈并进行改进,不必等到开发 ...
- 慕课网python分布式爬虫打造搜索引擎视频中爬取伯乐网文章
代码:https://github.com/longbigbeard/scrapy_demo
- BW 转换字符空格问题
早上忙了我一早上,以前写的一个季度判断的问题, 首先是调试,不断的调试DTP.让我头晕眼花. 首先关于空格问题,我自我批评,愚蠢的定义成STRING 类型,然后相互加减出现问题.应该定义成内部的日期格 ...
- 如何学习html画布呢(canvas)
我列出了canvas教学资源 http://www.gbtags.com/gb/gbliblist/1.htm 这是极客标签(不是极客学院) http://study.163.com/course/ ...
- 浅谈MariaDB Galera Cluster架构
MariaDB MariaDB 是由原来 MySQL 的作者Michael Widenius创办的公司所开发的免费开源的数据库服务器,MariaDB是同一MySQL版本的二进制替代品 ...
- LeetCode OJ:Binary Tree Paths(二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- New Concept English three (57)
28w/m 54errors I stopped to let the car cool off and to study the map. I had expected to be near my ...
- uva11997 K Smallest Sums&&UVALive 3135 Argus(优先队列,多路归并)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- ACM学习历程—HDU5668 Circle(数论)
http://acm.hdu.edu.cn/showproblem.php?pid=5668 这题的话,假设每次报x个,那么可以模拟一遍, 假设第i个出局的是a[i],那么从第i-1个出局的人后,重新 ...