zen*_*nna 5 javascript xss cross-domain http-headers cors
我试图在客户端和apache服务的python(django)服务器上使用jQuery(1.7.1)驱动的ajax设置简单的跨源资源共享.根据我读过的所有说明我的标题设置正确,但我不断收到以下错误:
XMLHttpRequest无法加载http://myexternaldomain.com/get_data.原产地的http://本地主机:8080没有被访问控制允许来源允许的.
我想要的标题(我不确定它甚至超过浏览器)发送是:
Request URL:http://myexternaldomain.com/get_data
Accept:application/json, text/javascript, */*; q=0.01
Origin:http://localhost:8080
Referer:http://localhost:8080/static/js/test-zetta.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11
Run Code Online (Sandbox Code Playgroud)
javascript代码是
var request = $.ajax({
url : "http://myexternaldomain.com/get_data",
type : "POST",
dataType : "json",
crossDomain : true
});
Run Code Online (Sandbox Code Playgroud)
请注意,origin设置正确.服务器Access-Control-Allow-Origin = *使用以下python代码添加标头
def process_response(self, response):
if response.has_header('Access-Control-Allow-Origin'):
return response
response['Access-Control-Allow-Origin'] = '*'
return response
def get_orders(request):
""" Tell worker what to do """
response_data = {}
response_data['action'] = 'probe'
response = process_response(HttpResponse(json.dumps(response_data), mimetype="application/json"))
return response
Run Code Online (Sandbox Code Playgroud)
如果我直接访问该地址,则似乎确认正确设置了标头
Access-Control-Allow-Origin:*
Content-Type:application/json
Date:Thu, 08 Mar 2012 05:06:25 GMT
Server:Apache/2.2.20 (Ubuntu)
Transfer-Encoding:chunked
Run Code Online (Sandbox Code Playgroud)
但是在跨域设置中它始终失败(尝试使用chrome和firefox).我已经尝试完全根据此问题的选定答案实现代码,但得到相同的错误
更新
我很确定问题是服务器端,因为我设法让我的ajax调用与不同的公共CORS服务器一起工作.当我比较从这个公共服务器返回的标头和从我的网站返回的标头时(当我从同一个域测试时),我看不出任何可以解释差异的重大差异(见下文).
我排除的一个微妙之处,可能或可能是重要的是实际域是多个子域的亚马逊域.的真实地址是http://ec2-23-20-27-108.compute-1.amazonaws.com/get_orders,随时探测它,看看有什么我做错了.
从公共服务器
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:622
Content-Type:text/html
Date:Thu, 08 Mar 2012 15:33:20 GMT
Keep-Alive:timeout=15, max=99
Server:Apache/2.2.14 (Ubuntu)
Vary:Accept-Encoding
X-Powered-By:Perl/5.8.7, PHP/4.4.0
Run Code Online (Sandbox Code Playgroud)
从我的服务器 - (不工作跨域)
Access-Control-Allow-Origin:*
Content-Encoding:gzip
Content-Type:text/plain
Date:Thu, 08 Mar 2012 15:32:24 GMT
Server:Apache/2.2.20 (Ubuntu)
Transfer-Encoding:chunked
Vary:Accept-Encoding
Run Code Online (Sandbox Code Playgroud)
您必须实施"预先发布"的请求和响应,因为您的情况被视为" 不那么简单 "的请求.仅需要Origin标头的基本CORS只能具有"application/x-www-form-urlencoded","multipart/form-data"和"text/plain"的内容类型.由于您返回"application/json",因此不符合此要求.
我对Django一无所知,但我发现通过使用Tomcat过滤器在我的应用程序之外实现CORS支持更容易.看起来你可以用Django 做同样的事情.
2013-08-11:看起来GitHub回购不再与我们在一起了.但是Django包看起来仍然可以在https://pypi.python.org/pypi/django-cors/0.1上找到
| 归档时间: |
|
| 查看次数: |
13433 次 |
| 最近记录: |