我目前正在开发一个 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
发布评论
评论(2)
这是不可能的:当您执行远程脚本时,它会在包含文档的上下文中运行。
有一些流行的解决方法:
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)
同源策略基于主机文档而不是脚本本身。
您需要使用跨域ajax技术。
The same origin policy is based on the host document not the script itself.
You need to use a cross domain ajax technique.