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

2023-01-03php开发问题
2

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17