惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 夜雨竹林

asp mvc3资料 设计模式之命令模式 设计模式之模板方法和策略模式的区别(二) 面向对象的分析和设计遵循的原则 设计模式之模板方法和策略模式的区别(一) 系统分析师网上内容推荐 英语学习资料 Repository模式 战略性设计之上下文 moq英文官方资料 moq中文介绍 领域驱动设计软件核心复杂性应对之道速查 UML用例图中包含(include)、扩展(extend)和泛化(generalization)三种 领域驱动设计软件核心复杂性应对之道 studyurl aspdotnet部分资源 Asp.net MVC权限设计思考 asp.net小技巧 标准的ASP.NET名称空间
研究 asp.net mvc2 ajax 原理
夜雨竹林 · 2010-03-22 · via 博客园 - 夜雨竹林

http://www.cnblogs.com/RChen/archive/2010/02/03/1662874.html

首先,System.Web.Mvc.Ajax 名称空间下,有一个静态辅助类,叫做 AjaxExtensions.

他有一系列 ActionLink 方法的重载形式,已供在 html 里使用,方便的生成各种 ajax 链接.
最终,这些链接都生成类似下面的一个 js 调用:
(其中 {0} 的位置是传入的叫做 ajaxOptions 的 JSON 格式的参数集合)

Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});

那么,接下来把目光转到相应的 js 文件。

在下列文件中,
<项目名>\Scripts\MicrosoftMvcAjax.debug.js

依次追踪函数调用如下:
AsyncHyperlink.handleClick()
_asyncRequest();
Sys.Mvc.MvcHelpers._onComplete()

在 _onComplete() 函数中,会判断 response 的 header, 决定是执行 js 还是更新元素:

if ((contentType) && (contentType.indexOf('application/x-javascript') !== -1)) {
    eval(ajaxContext.get_data());
}
else {
    Sys.Mvc.MvcHelpers.updateDomElement(ajaxContext.get_updateTarget(), ajaxContext.get_insertionMode(), ajaxContext.get_data());
}

在这之后,有一些简单的代码用来隐藏 loading panel,以及调用 onSuccess 或 onFail 的回调函数等,比较
容易理解,不多说了。

以上是普通 ajax 超链接的情况,下面简单的看一下 ajax form.
在 AjaxExtensions 辅助类中,同样有一堆 BeginForm 方法,以供在 html 中使用。
他们调用了上面 js 文件中的下列函数:

Sys.Mvc.AsyncForm.handleSubmit()

而这个函数的作用,是把 form 里的所有字段值序列化为一个字符串,然后仍然是通过上述的 _asyncRequest()
js 函数来执行,原理类似。
下面是关键的跟踪代码:

Sys.Mvc.AsyncForm.handleSubmit = function Sys_Mvc_AsyncForm$handleSubmit(form, evt, ajaxOptions) {
    evt.preventDefault();
    var body = Sys.Mvc.MvcHelpers._serializeForm(form);
    Sys.Mvc.MvcHelpers._asyncRequest(form.action, form.method || 'post', body, form, ajaxOptions);
}