跨域AJAX [英] cross domain ajax

查看:124
本文介绍了跨域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(访问控制 - 允许 - 起源,例如);
 

我总是得到这个错误。 原产地空不受访问控制 - 允许 - 产地允许的。如何配置呢?非常感谢你。

解决方案
  1. 您需要使用访问控制 - 允许 - 原产地,不是访问 - 控制 - 允许 - 产地
  2. 在我不知道的 self.response.headers.add_header(STR)是有效的,也许 self.response.headers.add_header(键,名称)
  3. * 站点无法正常工作(至少不是所有的浏览器)。你必须使用准确的域名,姓名,与协议。像 http://example.com
  4. 您需要原产地头,为Ajax调用。我不知道如何配置原始 XHR ,但我想,它的通过浏览器本身做的,你不能修改这个值。不管怎么说,你的域名不是例如
  5. 请不要忘了,它不适合大多数的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

解决方案

  1. You need to use Access-Control-Allow-Origin, not Access - Control - Allow - Origin
  2. I'm not sure that self.response.headers.add_header(str) is valid, maybe self.response.headers.add_header(key, name)?
  3. * domain doesn't work (at least not for all browsers). You have to use exact domain, full name, with protocol. Like http://example.com
  4. You need Origin header, for ajax call. I'm not sure how to configure raw xhr, but I guess that it's made by browser itself, and you can't modify this value. Anyway, your domain not example
  5. Don't forget that it doesn't work for most of IEs and Opera browsers.

这篇关于跨域AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆