在 Rails 3 应用程序中使用代理规避浏览器同源策略
我正在寻找一种可以使用多个远程 XML 服务、传递动态请求参数并将响应输出为 XML 或 JSON 的 Rails 解决方案。
我研究过 TinyProxy(无法通过 macports 将其安装在 OSX 上)和 Nginx。 Nginx 看起来可以满足我的需要,并且还可以为我们提供负载平衡等方面的灵活性。
还有其他人有这方面的经验吗?有经过尝试和测试的解决方案吗?
I'm looking for a rails solution that can consume multiple remote XML services, passing dynamic request parameters and outputting the response as XML or JSON.
I've looked into TinyProxy (Can't get it to install on OSX via macports) and also Nginx. Nginx looks like it will do what I need and also give us flexibility going forward with load balancing etc.
Has anyone else got any experience of this? Any tried and tested solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
围绕同源策略的标准解决方案之一是动态脚本标签和 JSON 回调,而不是通过代理。
例如:您的页面想要查询remotesite.com上的API,并且您尝试对http进行ajax调用://remotesite.com/api?query=list 但您收到同源错误。为了规避限制,您可以向 DOM 添加一个脚本标记(使用 JS),该标记指向如下所示的 url:
然后浏览器将运行该请求 - 与您尝试通过 ajax 调用执行的操作相同。现在的问题是,您需要让响应调用您的 js 函数之一,并将数据作为参数返回。因此,请求将返回类似以下内容:
现在,在您的代码中,您将有一个如下所示的函数:
In stead of going through a proxy, one of the standard solutions around the same-origin policy is dynamic script tags and JSON callbacks.
For example: you page page wants to query an API at remotesite.com and you try to do an ajax call to http://remotesite.com/api?query=list but you get the same-origin error. To circumvent the restriction you could add a script tag to the DOM (using JS) that points to the url like this:
The browser would then run that request - the same thing you tried to do w/ an ajax call. Now the catch is that you need to have the response call one of your js functions w/ the data returned as a argument. So the request would return something like:
Now in your code you'd have a function like this: