简体   繁体   English

有什么理由不使用JSONP吗?

[英]Is there any reason to not use JSONP?

I am developing a REST client which will talk to a project management service called attask . 我正在开发一个REST客户端,它将与名为attask的项目管理服务对话

Their REST API is convenient because it returns simple JSON that is easy to use in my code. 他们的REST API很方便,因为它返回的简单JSON易于在我的代码中使用。 Because of this I decided to build the interface with jQuery. 因此,我决定使用jQuery构建接口。 I quickly discovered that I cannot use AJAX from my site to the Attask API because of the Same Origin Policy . 我很快发现,由于同源策略的缘故,我无法从站点使用Atja API的AJAX。

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. 我的第一个想法是使用RESTSharp开发服务器端的REST客户端,该客户端将充当我的JavaScript和Attask API之间的桥梁。

Before I could get started with that implementation I discovered JSONP . 在开始该实现之前,我发现了JSONP This is new to me. 这对我来说是新的。 It turns out that the Attask API supports JSONP. 事实证明Attask API支持JSONP。 jQuery supports JSONP natively so suddenly I'm back to making a complete jQuery interface with no need for server-side intervention. jQuery本身就支持JSONP,所以突然我又回到了不需要服务器端干预的完整jQuery接口。

My question is, is there a reason not to use JSONP? 我的问题是,有理由不使用JSONP吗? Would there be any benefit to going the extra mile and building the server-side REST client and using real AJAX calls? 加倍努力并构建服务器端REST客户端并使用真正的AJAX调用会不会有任何好处?

Just bear in mind that JSONP is exactly what you're looking for: a workaround for the Same Origin Policy. 请记住,JSONP正是您要找的东西: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. 缺点是,如果该域决定违反您的信任,则他们现在可以在您的网页上运行任意javascript代码,从而允许他们将所需的任何信息发送到自己的服务器。

If you trust 'em, go for it. 如果您信任他们,那就去吧。 If you don't, set up your own server-side proxy. 如果不这样做,请设置自己的服务器端代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM