問題描述
我有一個正在構建的自卷式 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 :
- 如果通過正常"請求調用,則返回完整頁面
- 或者如果通過 Ajax 請求調用,則只返回一些數據(或頁面的一部分)
注意:這不是一個瘋狂"的想法;例如,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 調用 - 在哪里處理它們?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!