避免 x 域解决方案

发布于 2024-09-30 18:10:30 字数 233 浏览 0 评论 0 原文

我目前正在开发一个 Web 应用程序,客户可以通过将 javascript 链接添加到我的服务器上的 js 文件来将其添加到他们的网页中。该应用程序从我的服务器读取所有 javascript 文件,但在尝试使用 ajax 从数据库获取数据时仍然收到错误。我认为这不会成为问题,因为这些文件位于我的服务器上。

我可以解决这个问题还是必须制定跨浏览器解决方案?我对客户服务器没有任何控制权。

预先感谢

迈克尔

I'm currently working on a web application that customers can add to their webpages by adding a javascript link to a js file on my server. The application read all the javascriptfiles from my sever, but I still get an error when trying to use ajax to get data from my database. I didn't think that would be a problem because the files is on my server.

Can I fix this or do I have to make a cross-browser solution? I don't have any control over the costumers server.

Thanks in advance

Mikael

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

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

发布评论

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

评论(2

绮筵 2024-10-07 18:10:30

这是不可能的:当您执行远程脚本时,它会在包含文档的上下文中运行。

有一些流行的解决方法:

  • iframe,它可以解决跨域问题,但不能与远程站点很好地集成(例如,没有自定义样式)

  • 使用JSONP 进行跨域 Ajax 请求(详细解释此处)

  • 使用服务器端代理脚本(在此场景中不是一个选项)

  • 使用 YQL (我对此不熟悉,但据说可以工作)

This is not possible: When you execute a remote script, it runs in the context of the containing document.

There are some popular workarounds for this:

  • Using an iframe, which fixes the cross-domain problem but doesn't integrate well with the remote site (e.g. no custom styling)

  • Using JSONP to make cross-domain Ajax requests (detailed explanation here)

  • Using a server-side proxy script (not an option in this scenario)

  • Using YQL (I'm not familiar with this but it's said to work)

待"谢繁草 2024-10-07 18:10:30

同源策略基于主机文档而不是脚本本身。

您需要使用跨域ajax技术。

The same origin policy is based on the host document not the script itself.

You need to use a cross domain ajax technique.

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