有什么理由不使用 JSONP 吗?
我正在开发一个 REST 客户端,它将与名为 attask 的项目管理服务进行通信。
他们的 REST API 很方便,因为它返回简单的 JSON,很容易在我的代码中使用。因此,我决定使用 jQuery 构建界面。我很快发现,由于同源政策。
我的第一个想法是使用 RESTSharp 开发一个服务器端 REST 客户端,它将充当我的 javascript 和任务 API。
在开始实现该实现之前,我发现了 JSONP。这对我来说是新的。事实证明,Attask API 支持 JSONP。 jQuery 本身就支持 JSONP,所以我突然又开始制作一个完整的 jQuery 界面,而无需服务器端干预。
我的问题是,有理由不使用 JSONP 吗?更加努力地构建服务器端 REST 客户端并使用真正的 AJAX 调用会有什么好处吗?
I am developing a REST client which will talk to a project management service called attask.
Their REST API is convenient because it returns simple JSON that is easy to use in my code. Because of this I decided to build the interface with jQuery. I quickly discovered that I cannot use AJAX from my site to the Attask API because of the Same Origin Policy.
My first thought was to develop a server-side rest client using RESTSharp that would act as a bridge between my javascript and the Attask API.
Before I could get started with that implementation I discovered JSONP. This is new to me. It turns out that the Attask API supports JSONP. jQuery supports JSONP natively so suddenly I'm back to making a complete jQuery interface with no need for server-side intervention.
My question is, is there a reason not to use JSONP? Would there be any benefit to going the extra mile and building the server-side REST client and using real AJAX calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,JSONP 正是您正在寻找的:同源策略的解决方法。随之而来的是同源策略旨在避免的所有安全问题。好处是您可以选择您信任的特定域。缺点是,如果该域决定违反您的信任,他们现在可以在您的网页上运行任意 JavaScript 代码,从而允许他们将任何他们想要的信息发送到自己的服务器。
如果你信任他们,那就去吧。如果不这样做,请设置您自己的服务器端代理。
Just bear in mind that JSONP is exactly what you're looking for: a workaround for the Same Origin Policy. With that will come all the security problems that the Same Origin Policy was intended to avoid. The upside is that you get to choose a specific domain that you trust. The downside is, if that domain decides to violate your trust they can now run arbitrary javascript code on your web pages, allowing them to send any information they want to their own servers.
If you trust 'em, go for it. If you don't, set up your own server-side proxy.