由asp.net mvc的MicrosoftMvcAjax中主要要以下两个ajax类型:
- Ajax.ActionLink
- Ajax.FormBegin
由Ajax.ActionLink产生的html代码(mvc示例:ContantManager)如下:
代码
<
a
groupid
="23"
href
="/Contact/Index/23"
onclick ="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event),
{ insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'divContactList1',
onBegin: Function.createDelegate(this, beginContactList),
onFailure: Function.createDelegate(this, failureContactList),
onSuccess: Function.createDelegate(this, successContactList) });" >
private string
</ a >
onclick ="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event),
{ insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: 'divContactList1',
onBegin: Function.createDelegate(this, beginContactList),
onFailure: Function.createDelegate(this, failureContactList),
onSuccess: Function.createDelegate(this, successContactList) });" >
private string
</ a >
当点击此链接,服务器执行成功调用onSuccess回调函数,执行失败调用onFailure回调函数。
onSuccess函数与onFailure函数的原型如下:
ajaxOptions.onSuccess(ajaxContext);
ajaxOptions.onFailure(ajaxContext);
这两个函数都有一个参数ajaxContext。
参数ajaxContext用法说明:
- 显示服务器返回内容: alert(ajaxContext.get_data());
- 服务器返回内容类型:alert(ajaxContext.get_response().getResponseHeader('Content-Type'));
参数ajaxContext代码如下:
文件MicrosoftMvcAjax.debug.js第33行
代码
//
//
// Sys.Mvc.AjaxContext
Sys.Mvc.AjaxContext = function Sys_Mvc_AjaxContext(request, updateTarget, loadingElement, insertionMode) {
// / <param name="request" type="Sys.Net.WebRequest">
// / </param>
// / <param name="updateTarget" type="Object" domElement="true">
// / </param>
// / <param name="loadingElement" type="Object" domElement="true">
// / </param>
// / <param name="insertionMode" type="Sys.Mvc.InsertionMode">
// / </param>
// / <field name="_insertionMode" type="Sys.Mvc.InsertionMode">
// / </field>
// / <field name="_loadingElement" type="Object" domElement="true">
// / </field>
// / <field name="_response" type="Sys.Net.WebRequestExecutor">
// / </field>
// / <field name="_request" type="Sys.Net.WebRequest">
// / </field>
// / <field name="_updateTarget" type="Object" domElement="true">
// / </field>
this ._request = request;
this ._updateTarget = updateTarget;
this ._loadingElement = loadingElement;
this ._insertionMode = insertionMode;
}
Sys.Mvc.AjaxContext.prototype = {
_insertionMode: 0 ,
_loadingElement: null ,
_response: null ,
_request: null ,
_updateTarget: null ,
get_data: function Sys_Mvc_AjaxContext$get_data() {
// / <value type="String"></value>
if ( this ._response) {
return this ._response.get_responseData();
}
else {
return null ;
}
},
get_insertionMode: function Sys_Mvc_AjaxContext$get_insertionMode() {
// / <value type="Sys.Mvc.InsertionMode"></value>
return this ._insertionMode;
},
get_loadingElement: function Sys_Mvc_AjaxContext$get_loadingElement() {
// / <value type="Object" domElement="true"></value>
return this ._loadingElement;
},
get_response: function Sys_Mvc_AjaxContext$get_response() {
// / <value type="Sys.Net.WebRequestExecutor"></value>
return this ._response;
},
set_response: function Sys_Mvc_AjaxContext$set_response(value) {
// / <value type="Sys.Net.WebRequestExecutor"></value>
this ._response = value;
return value;
},
get_request: function Sys_Mvc_AjaxContext$get_request() {
// / <value type="Sys.Net.WebRequest"></value>
return this ._request;
},
get_updateTarget: function Sys_Mvc_AjaxContext$get_updateTarget() {
// / <value type="Object" domElement="true"></value>
return this ._updateTarget;
}
}
// Sys.Mvc.AjaxContext
Sys.Mvc.AjaxContext = function Sys_Mvc_AjaxContext(request, updateTarget, loadingElement, insertionMode) {
// / <param name="request" type="Sys.Net.WebRequest">
// / </param>
// / <param name="updateTarget" type="Object" domElement="true">
// / </param>
// / <param name="loadingElement" type="Object" domElement="true">
// / </param>
// / <param name="insertionMode" type="Sys.Mvc.InsertionMode">
// / </param>
// / <field name="_insertionMode" type="Sys.Mvc.InsertionMode">
// / </field>
// / <field name="_loadingElement" type="Object" domElement="true">
// / </field>
// / <field name="_response" type="Sys.Net.WebRequestExecutor">
// / </field>
// / <field name="_request" type="Sys.Net.WebRequest">
// / </field>
// / <field name="_updateTarget" type="Object" domElement="true">
// / </field>
this ._request = request;
this ._updateTarget = updateTarget;
this ._loadingElement = loadingElement;
this ._insertionMode = insertionMode;
}
Sys.Mvc.AjaxContext.prototype = {
_insertionMode: 0 ,
_loadingElement: null ,
_response: null ,
_request: null ,
_updateTarget: null ,
get_data: function Sys_Mvc_AjaxContext$get_data() {
// / <value type="String"></value>
if ( this ._response) {
return this ._response.get_responseData();
}
else {
return null ;
}
},
get_insertionMode: function Sys_Mvc_AjaxContext$get_insertionMode() {
// / <value type="Sys.Mvc.InsertionMode"></value>
return this ._insertionMode;
},
get_loadingElement: function Sys_Mvc_AjaxContext$get_loadingElement() {
// / <value type="Object" domElement="true"></value>
return this ._loadingElement;
},
get_response: function Sys_Mvc_AjaxContext$get_response() {
// / <value type="Sys.Net.WebRequestExecutor"></value>
return this ._response;
},
set_response: function Sys_Mvc_AjaxContext$set_response(value) {
// / <value type="Sys.Net.WebRequestExecutor"></value>
this ._response = value;
return value;
},
get_request: function Sys_Mvc_AjaxContext$get_request() {
// / <value type="Sys.Net.WebRequest"></value>
return this ._request;
},
get_updateTarget: function Sys_Mvc_AjaxContext$get_updateTarget() {
// / <value type="Object" domElement="true"></value>
return this ._updateTarget;
}
}