使用jQuery和Tornado实现跨源资源共享(CORS)
cross-origin resource sharing (CORS) with jQuery and Tornado
比方说,我有一个Tornado web服务器(localhost)和一个网页(othermachine.com),后者包含需要对Tornado服务器进行跨域ajax调用的javascript。
所以我把我的龙卷风设置成这样:
class BaseHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "http://www.othermachine.com")
self.set_header("Access-Control-Allow-Credentials", "true")
self.set_header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
self.set_header("Access-Control-Allow-Headers",
"Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, Cache-Control")
我的javascript进行了一个jQuery调用:
$.ajax({
type: 'GET',
url: "http://localhost:8899/load/space",
data: { src: "dH8b" },
success: function(resp){
console.log("ajax response: "+resp);
},
dataType: 'json',
beforeSend: function ( xhr ) {
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.setRequestHeader('Access-Control-Request-Method', 'GET');
xhr.setRequestHeader('Access-Control-Request-Headers', 'X-Requested-With');
xhr.withCredentials = true;
}
});
但我得到了可爱的XMLHttpRequest cannot load http://localhost:8899/load/space?src=dH8b. Origin http://www.othermachine.com is not allowed by Access-Control-Allow-Origin
错误。我无法判断jQuery/Tornado(或两者都有)的哪一侧设置不正确。
根据开发工具,以下是jQuery请求发送的标题:
请求标头
Accept:*/*
Origin:http://www.othermachine.com
Referer:http://www.othermachine.com/athletes.html?src=BCYQ&msgid=6xjb
User-Agent:Mozilla/5.0 ...
如果我只是从浏览器的url字段发出请求,我会得到一个"200 OK":
响应标头
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type, User-Agent, X-Requested-With, X-Requested-By, Cache-Control
Access-Control-Allow-Methods:GET,POST
Access-Control-Allow-Origin:http://www.othermachine.com
Content-Length:0
Content-Type:text/html; charset=UTF-8
Server:TornadoServer/2.2.1
这是否意味着龙卷风正在发挥作用?我试图遵循所有stackoverflow CORS+jQuery帖子的建议(例如这个),但没有成功。CORS在概念上似乎很简单,但也许我从根本上误解了CORS事务中应该发生的事情。。。请帮忙!提前谢谢。
Nevermind,编码太晚和太长会导致人们被打字错误大小的东西绊倒。记录在案,这就是jQuery:所需要的全部内容
var data = { msgid: "dH8b" },
url = "http://localhost:8899/load" + '?' + $.param(data);
$.getJSON( url, function(resp){
console.log("ajax response: "+resp+" json="+JSON.stringify(resp));
});
这就是你所需要的龙卷风:
class BaseHandler(tornado.web.RequestHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "http://www.othermachine.com")
使用jQuery 1.7.2,Tornado 2.2.1。
尝试将原点设置为:othermachine.com
。它应该是域名,而不是网站地址
相关文章:
- Angularjs 跨源资源共享 (CORS) 发布或放置方法在 IE8 和 IE9 中不起作用
- THREE.js:跨来源资源共享策略拒绝跨来源映像加载
- 使用jQuery和Tornado实现跨源资源共享(CORS)
- 跨域资源共享 (CORS) 是否区分 HTTP 和 HTTPS
- RGBaster - 来自源的映像已被跨源资源共享策略阻止加载
- 跨域资源共享(CROS)似乎不适用于PhoneGap中的Windows Phone 8
- 解决与烧瓶的跨源资源共享问题
- 解决跨站点脚本资源共享问题
- 跨来源资源共享策略拒绝跨来源映像加载
- 如何在我的移动应用程序(cordova)上启用跨域资源共享,它连接到API(在Yii中)
- 跨域资源共享(CORS)概念
- IE10和跨域资源共享(CORS)问题与图像/画布
- 为什么没有更多的oEmbed提供商在其端点上启用跨域资源共享?
- 如何配置以Joomla为服务器的跨域资源共享(Cross Origin Resource Sharing, CORS)
- 跨域资源共享的XMLHttpRequest问题
- 从浏览器托管的JavaScript应用到自定义服务器的跨域资源共享
- 如何通过授权信息有选择地允许跨域资源共享
- Tumblr 上的 JS:“跨域资源共享策略”错误
- 跨域资源共享GET: '拒绝获取不安全的标题"etag"'从响应
- 如何使用ajax POST实现跨域资源共享(CORS)