跨域AJAX [英] cross domain ajax
问题描述
在我读<一href="http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/"相对=nofollow>这个文章,我的理解是,为了让跨域Ajax调用,我必须设置服务器响应是的访问控制 - 允许 - 原产地* 的(公开测试目的),这里是我的服务器code,谷歌AppEngine上在Python
self.response.headers.add_header(访问 - 控制 - 允许 - 产地:*)
self.response.headers.add_header(内容类型,应用/ JSON的,字符集=utf-8')
self.response.out.write(simplejson.dumps(的JSONObject))
我不知道这是否是正确的。而我的Ajax调用
xhr.open(得,http://example.com,真正的);
xhr.setRequestHeader(访问控制 - 允许 - 起源,例如);
我总是得到这个错误。 原产地空不受访问控制 - 允许 - 产地允许的。的如何配置呢?非常感谢你。
- 您需要使用
访问控制 - 允许 - 原产地
,不是访问 - 控制 - 允许 - 产地
- 在我不知道的
self.response.headers.add_header(STR)
是有效的,也许self.response.headers.add_header(键,名称)
? -
*
站点无法正常工作(至少不是所有的浏览器)。你必须使用准确的域名,姓名,与协议。像http://example.com
- 您需要
原产地
头,为Ajax调用。我不知道如何配置原始XHR
,但我想,它的通过浏览器本身做的,你不能修改这个值。不管怎么说,你的域名不是例如
- 请不要忘了,它不适合大多数的IE和Opera浏览器的工作。
After I read this article, what I understand is that in order to allow cross-domain ajax calls, I have to set the server response to be Access-Control-Allow-Origin: *(public for testing purpose), and here is my server code, Google AppEngine in Python
self.response.headers.add_header('Access - Control - Allow - Origin:*')
self.response.headers.add_header('content-type', 'application/json', charset = 'utf-8')
self.response.out.write(simplejson.dumps(Jsonobject))
I don't know if that is correct. And my Ajax call
xhr.open("get", "http://example.com", true);
xhr.setRequestHeader("Access-Control-Allow-Origin","example");
I always got this error. Origin null is not allowed by Access-Control-Allow-Origin. How do I configure this? Thank you very much
- You need to use
Access-Control-Allow-Origin
, notAccess - Control - Allow - Origin
- I'm not sure that
self.response.headers.add_header(str)
is valid, maybeself.response.headers.add_header(key, name)
? *
domain doesn't work (at least not for all browsers). You have to use exact domain, full name, with protocol. Likehttp://example.com
- You need
Origin
header, for ajax call. I'm not sure how to configure rawxhr
, but I guess that it's made by browser itself, and you can't modify this value. Anyway, your domain notexample
- Don't forget that it doesn't work for most of IEs and Opera browsers.
这篇关于跨域AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!