通过异步控制器从浏览器进行 Ajax 调用

发布于 2024-10-12 19:45:59 字数 286 浏览 7 评论 0原文

我正在调用外部 REST 服务 (Vimeo REST API)。服务的响应是 JSON 对象。我的应用程序中的单个视图可能最终会多次调用该服务(用于加载多个视频)。

我想衡量在这种情况下使用以下架构的优缺点,

  1. 使用 jquery ajax 调用加载单个视频(从浏览器调用 REST 服务,因为每个视频都有其 Id,这是获取详细信息所需的唯一信息)
  2. 使用异步控制器操作在控制器中进行 REST 调用,然后显示视频

注意:我使用的是不需要身份验证的简单 API 服务。

I am calling an external REST service (Vimeo REST API). The response of the service is JSON object. A single view in my application might end up making multiple calls to the service (for loading multiple videos).

I wanted to gauge the Pros and Cons of using the following architecture in this situation

  1. using jquery ajax calls for loading individual videos (call made to the REST service from the browser as each video has its Id which is the only thing needed to get the details)
  2. using ansynchronous controller action to make the REST call in the controller and then displaying the vedios

Note: I am using simple API services which do not require authentication.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日 2024-10-19 19:45:59

AsyncController 并不是为了异步服务 HTTP 请求而设计的,而是为了执行长时间运行的服务器端进程。向 REST 服务发出单个请求可能是也可能不是长时间运行的服务器端进程。

因此,无论您决定在服务器端发出 REST 请求还是直接从客户端(浏览器)发出 REST 请求,都不一定需要使用 AsyncController。普通的控制器就可以完成这项工作。

您应如何处理视频请求取决于您的业务层的结构。如果了解要在业务层处理的 Vimeo 视频,那么最佳实践是让您的 Web 服务调用服务端。否则,您的客户端上会有业务逻辑,这可能会使维护变得困难。

另一方面,如果您的 Vimeo 视频只是独立 UI 小部件的一部分,那么可以安全地在客户端上完全处理请求,而不会产生意外后果。

我假设对 Vimeo 的 Web 服务调用收到 Flash 文件或类似文件。从服务器进行 Vimeo 服务调用需要更多带宽并消耗更多内存,因为数据必须传输到您的服务器。

如果您在服务器端执行此操作,则会发生这种情况:

 1 - Browser sends HTTP-Request to YourApplication
 2 - YourApplication sends HTTP-Request to Vimeo's WebService
 3 - Vimeo's WebService sends big HTTP Response with the Video data to YourApplication
 4 - YourApplication sends big HTTP Response with the Video data to Browser

 * If you choose to do it this way, this might be the point at which it makes sense to use an AsyncController

如果您在客户端执行此操作,则会发生这种情况:

 1 - Browser sends HTTP-Request to Vimeo's WebService
 2 - Vimeo's WebService sends big HTTP Response with the Video data to the Browser

这使得看起来在客户端执行所有操作更好。但随之而来的是整个业务逻辑问题。可以通过向同步控制器操作发送 ajax 请求来进行业务逻辑处理,并使其向浏览器返回对 REST 服务的调用的 URL 来解决此问题。所以:

 1 - Browser sends AJAX request to YourApplication
 2 - YourApplication handles business logic and sends the URL of the REST request to Browser
 3 - Browser sends AJAX request to Vimeo's WebService
 4 - Vimeo's WebService sends big HTTP response with the video data to the browser.

我认为这可能是你最好的选择。

AsyncController was not designed for the purpose of serving HTTP requests asynchronously, but for the purpose of executing long running server-side processes. Making a single request to REST service may or may not be a long running server-side process.

So, whether you decide to make the REST request server-side or directly from the client (browser), you do not necessarily need to use an AsyncController. A normal Controller might do the job.

How you should handle the video requests depends on how your business layer is structured. If there is knowledge of the Vimeo videos for processing in the business layer, then it is best practice to make your web-service calls service side. Otherwise, you have business logic on your client, which can make things difficult to maintain.

If on the other hand, your Vimeo videos are only part of a self-contained UI-widget, then it is safe to process the request completely on the client without incurring unexpected consequences.

I am assuming the web-service call to Vimeo receives a Flash file or something like it. It would require more bandwidth as well as cost more memory to make the Vimeo service calls from the server, because then the data has to go to your server.

If you do it server side, this happens:

 1 - Browser sends HTTP-Request to YourApplication
 2 - YourApplication sends HTTP-Request to Vimeo's WebService
 3 - Vimeo's WebService sends big HTTP Response with the Video data to YourApplication
 4 - YourApplication sends big HTTP Response with the Video data to Browser

 * If you choose to do it this way, this might be the point at which it makes sense to use an AsyncController

If you do it client side, this happens:

 1 - Browser sends HTTP-Request to Vimeo's WebService
 2 - Vimeo's WebService sends big HTTP Response with the Video data to the Browser

This makes it look like doing it all client-side is better. But then there is the whole business logic issue. This can be remedied by sending an ajax request to a synchronous controller action to do business logic processing, and having it return the URL of the call to the REST service to the browser. So:

 1 - Browser sends AJAX request to YourApplication
 2 - YourApplication handles business logic and sends the URL of the REST request to Browser
 3 - Browser sends AJAX request to Vimeo's WebService
 4 - Vimeo's WebService sends big HTTP response with the video data to the browser.

I think this is probably your best bet.

污味仙女 2024-10-19 19:45:59

第一种方法可能会出现问题,因为浏览器禁止跨域ajax调用(打开的页面来自yoursite.com域,并且您调用vimeo.com)。

除了第二种方法之外,您还可以使用 Vimeo API 提供的 JSONP: http://vimeo.com /api/docs/response-formats

You may have problems with the first method, because cross-domain ajax calls (the page opened is from yoursite.com domain, and you making a call to vimeo.com) are prohibited by the browsers.

In addition to the second method, you can use JSONP provided by Vimeo API: http://vimeo.com/api/docs/response-formats

听不够的曲调 2024-10-19 19:45:59

如果您为单个操作方法多次调用 Vimeo 的 REST 服务,那么这似乎是使用异步控制器的一个不错的选择。这有两个好处:它允许您并行执行对 Vimeo 服务的多个调用,并且它会释放正在处理请求的线程,并允许它在服务器等待 Vimeo 响应时处理其他请求。

我想这里的权衡是增加了客户端代码或控制器代码的复杂性。在服务器端发出请求(无论是否异步执行)的另一个好处是,您可以缓存数据并最大限度地减少将来处理请求时必须进行的昂贵 Web 服务调用的数量。这实际上取决于缓存数据在您的情况下是否是一个可行的选择。

If you are making multiple calls to Vimeo's REST service for a single action method, that would seem like a good candidate for using an asynchronous controller. This would have two benefits: it would allow you to execute multiple calls to Vimeo's service in parallel, and it would free up the thread that is handling the request and allow it to handle other requests while the server is waiting for a response from Vimeo.

I guess the trade-off here is adding complexity to your client code or to your controller code. Another benefit of making the requests on the server side (whether you do them asynchronously or not) is that you could potentially cache the data and minimize the number of expensive web service calls you have to make to handle requests in the future. That really depends on whether caching the data is a viable option in your situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文