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 ...
随机推荐
- QT Creator引用win32 api类库方法(.lib)
由于Qt Creator使用的是mingW进行程序编译,该编译方式无法识别#pragma comment(lib,"lib\\hvdailt.lib")引用. 所以需要在.Pro文 ...
- 如何学习Android系统源码(转)
一. Android系统的源代码非常庞大和复杂,我们不能贸然进入,否则很容易在里面迷入方向,进而失去研究它的信心.我们应该在分析它的源代码之前学习好一些理论知识,下面就介绍一些与Android系统相关 ...
- python编程实例-dmidecode系统信息搜集
#!/usr/bin/env python from subprocess import PIPE,Popen def getDmi(): p = Popen(['dmidecode'],stdout ...
- winform使用相对路径读取文件的方法
获取exe文件的路径进行截取,分两次进行,然后拼接文件名,形成全路径 代码如下: string haarXmlPath = @"haarcascade_frontalface_alt_tre ...
- stl_hash_set.h
stl_hash_set.h // Filename: stl_hash_set.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: ...
- Java 参数的和
public class CommandParamter { public static void main(String[] args) { // TODO Auto-generated metho ...
- java-07接口与继承
1.动手实验:继承条件下的构造方法调用 代码: package demo; class Grandparent{ public Grandparent(){ System.out.println(&q ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- ZIP 算法详解 (转!)
zip 的压缩原理与实现(lz77 算法压缩) 无损数据压缩是一件奇妙的事情,想一想,一串任意的数据能够根据一定的规则转换成只有原来 1/2 - 1/5 长度的数据,并且能够按照相应的规则还原到原来的 ...
- 11.Selenium+Python案例--百度
一.具体代码实现 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionCh ...