我有一个正在构建的自卷式 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.
我的问题是,通常在 MVC 中存储的 ajax 调用处理程序在哪里?我应该将它们存储在参与调用的同一个控制器中吗?
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(browse 是控制器,blogs 是方法)正在对更新的博客列表进行 AJAX 调用,该调用是否只是对 www.example 的调用.com/browse/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?
或者,它是一个单独的 AJAX-only 控制器吗?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.
一种半替代的想法可能是在您的控制器中只有一个操作:/browse/blogs
-- 并始终调用该操作.
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 :
注意:这不是一个疯狂"的想法;例如,Zend Framework 提供了一些东西来促进这一点(参见 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 )
这篇关于MVC ajax 调用 - 在哪里处理它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!