跨域ajax

发布于 2025-01-04 13:54:41 字数 793 浏览 4 评论 0原文

在我阅读 此内容后文章,我的理解是,为了允许跨域ajax调用,我必须将服务器响应设置为Access-Control-Allow-Origin: *< /strong>(公开测试目的),这是我的服务器代码,Python 中的 Google AppEngine

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))

我不知道这是否正确。我的 Ajax 调用

xhr.open("get", "http://example.com", true);
xhr.setRequestHeader("Access-Control-Allow-Origin","example");

总是收到此错误。 Access-Control-Allow-Origin 不允许 Origin null。我该如何配置?非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2025-01-11 13:54:41
  1. 您需要使用 Access-Control-Allow-Origin,而不是 Access - Control - Allow - Origin
  2. 我不确定 self.response.headers.add_header (str) 有效,也许是 self.response.headers.add_header(key, name)
  3. * 域不起作用(至少不适用于所有浏览器)。您必须使用准确的域名、全名和协议。就像 http://example.com
  4. 您需要 Origin 标头,用于 ajax 调用。我不知道如何配置原始xhr,但我猜它是浏览器本身制作的,你无法修改这个值。无论如何,您的域名不是 example
  5. 不要忘记它不适用于大多数 IE 和 Opera 浏览器。
  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.
﹏雨一样淡蓝的深情 2025-01-11 13:54:41

也许这些代码行可以解决您的问题。

您可以做一件事,只需设置 Access-Control-Allow-Origin & CustomeHeaders 中的 Access-Control-Allow-Headers 您的 Web 服务 web.config 文件。

 <add name="Access-Control-Allow-Origin" value="*" />
 <add name="Access-Control-Allow-Headers" value="Content-Type" />

如果您只想允许特定域,您可以使用域的特定值而不是 * 值来实现

May be these lines of code solve your problem.

You can do one thing for that just need to set Access-Control-Allow-Origin & Access-Control-Allow-Headers in CustomeHeaders your web service web.config file.

 <add name="Access-Control-Allow-Origin" value="*" />
 <add name="Access-Control-Allow-Headers" value="Content-Type" />

If you want to allow only for specific domain , you can do that with specific value of domain instead of * value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文