本文介绍了那么,JSONP 还是 CORS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 WebAPI 部署在 Intranet 环境中.这意味着安全不是我的问题.

My WebAPI was deployed in the Intranet environment. That means security was not my concern.

似乎 CORS 对客户更友好并且更容易实施.

It seems that CORS is much more friendly to the client and easier to implement.

我可能忽略的任何其他问题?

Any other concerns I might have missed?

推荐答案

这是一个相当广泛的问题,可以保证一个 wiki 本身.谷歌上也有很多关于这两者的内容,但我想我可以找到几个关键点.

This is a pretty broad question, and could warrant a wiki unto itself. There is also quite a bit on google regarding the two, but I think I can hit a few key points.

  • 如果您的服务器需要只读 ajax 接口,并且需要支持 IE
  • 另一方面,如果您的 Web API 是读/写(例如完整的 REST 或只是 POST/GET)而不是只读(即 GET),则 JSONP 已过时.使用 CORS.JSONP 本质上是只读的.

如果这些都不是问题,我会选择您最简单或最熟悉的方法.如果它是一个折腾,尝试 CORS,因为它是更现代"的解决方案,而 JSONP 更像是一种黑客,将数据转换为脚本以绕过跨域限制.但是,CORS 通常需要更多的服务器端配置.

If neither of these are a concern, I would just go with whatever is easiest or most familiar to you. If its a tossup, try CORS, since it is the more "modern" solution and JSONP is more of a hack, turning data into scripts to bypass cross-domain restrictions. CORS does however, typically require more server-side configuration.

如果您使用的是 jQuery,我不确定您是从哪里想到 CORS对客户更加友好并且更易于实施的想法强>."请参阅 https://gist.github.com/3131951.jQuery 抽象了 JsonP 的细节,而根据您使用的技术,在服务器端实现 CORS 实际上可能有些棘手.

If you're using jQuery, I'm not sure where you're coming up with the idea that CORS is "much more friendly to the client and easier to implement." See https://gist.github.com/3131951 . jQuery abstracts the details of JsonP, and CORS can actually be somewhat tricky to implment on your server-side depending on what technology you're using.

我最近开发了一个 Web 应用程序,使用 jquery 和backbone.js,它从我们控制的各种跨域 Web 服务中读取数据,最终使用 Json-P 而不是 CORS,因为我们需要支持 IE7,这是一个在服务器端稍微简单一点(我们使用 DjangoRestFramework 运行 Django),并且在客户端几乎与 jquery 相同.

I recently developed a web app, using jquery and backbone.js, which reads from various cross-domain web services that we control, and ended up using Json-P instead of CORS because we need to support IE7 and it was a bit simpler on the server side (we run Django w/ DjangoRestFramework), and virtually the same with jquery on the client side.

这篇关于那么,JSONP 还是 CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:54