MVC ajax 调用 - 在哪里处理它们?

MVC ajax calls - where to handle them?(MVC ajax 调用 - 在哪里处理它们?)
本文介绍了MVC ajax 调用 - 在哪里处理它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个正在构建的自卷式 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 调用 - 在哪里处理它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to make 5 random numbers with sum of 100(如何制作5个总和为100的随机数)
str_shuffle and randomness(str_shuffle 和随机性)
Algorithm for generating a random number(生成随机数的算法)
What#39;s the disadvantage of mt_rand?(mt_rand 的缺点是什么?)
What is the best way to generate a random key within PHP?(在 PHP 中生成随机密钥的最佳方法是什么?)
How to create a random string using PHP?(如何使用 PHP 创建随机字符串?)