MVC Ajax调用 - 在哪里处理?MVC、Ajax

2023-09-10 13:33:27 作者:无言的对白

我有一个自推出MVC框架,我建设,并且到现在为止已经成功地避免了任何AJAX调用的需要。但是现在,我想创建一个实时更新的饲料。

I have a self-rolled MVC framework that I am building, and up to this point have managed to avoid the need for any AJAX calls. Now, however, I'd like to create a real-time updating feed.

我的问题是,哪里的处理程序Ajax调用通常存放在一个MVC?我应该将它们存储在参与制作的呼吁,即在同一个控制器?

My question is, where are the handlers for the ajax calls usually stored in an MVC? Should I store them in the same controller that is involved in making the call?

例如,如果我的域名www.example.com/browse/blogs(浏览是控制器,博客是法)作出AJAX调用的博客的更新列表,会调用仅仅是www.example .COM /浏览/ update_list什么?

For example, if my domain www.example.com/browse/blogs (browse is the controller, blogs is the method) is making an AJAX call for an updated list of blogs, would the call simply be to www.example.com/browse/update_list or something?

OR,所以它是一个独立的AJAX的唯一的控制器? www.example.com/ajax/update_blogs

OR, so it be to a separate AJAX-only controller? www.example.com/ajax/update_blogs

你怎么办呢?

推荐答案

我会说一个Ajax请求是完全一样的非Ajax之一:它的工作原理完全相同的方式,实际上,从一个角度来看HTTP协议。

I'd say an Ajax request is exactly the same as a non-Ajax one : it works exactly the same way, actually, from a point of view of HTTP Protocol.

唯一的区别是,你正在返回一些非格式化的数据,如JSON或XML的(喂,这是一样的生成ATOM源^^)的,或者是只有一部分HTML页。

The only difference is that you are returning some non-formated data, as JSON or XML (hey, this is the same as generating an ATOM feed ^^ ), or only a portion of an HTML page.

所以,我会像对待那些作为任何其他正常的HTTP请求,并将它们放置的方式,我会为非Ajax请求。

So, I would treat those as any other "normal" HTTP request, and place them the way I would for non-Ajax requests.

一个半备用的想法可能是只有一个行动,你controlller: /浏览/博客 - 始终调用一个

A semi-alternate idea might be to have only one action in your controlller : /browse/blogs -- and always call that one.

不过,它会检测到,如果它通过Ajax请求或不是,并会:

But, it would detect if it's being via an Ajax request or not, and would :

通过一个正常的请求,返回一个完整的网页,如果名为 或仅返回一些数据(或页面的一部分),如果通过Ajax请求名为

请注意:这不是一个疯狂的想法; Zend框架,例如,提供了一些东西,方便了(见 12.8.4.3。ContextSwitch里和AjaxContext )

Note : that's not a "wild" idea ; Zend Framework, for instance, provides some stuff to facilitate that (see 12.8.4.3. ContextSwitch and AjaxContext )

 
精彩推荐
图片推荐