@Ajax.BeginForm() 助手利用 jQuery 不显眼的 AJAX 库。如果您检查帮助程序返回类型,它会返回 System.Web.Mvc.Html.MvcForm,与创建 <form> 标签的 @Html.BeginForm() 相同的返回类型:
public static MvcForm BeginForm(
this AjaxHelper ajaxHelper,
AjaxOptions ajaxOptions
)
由于它的所有重载都没有在 System.Web.Mvc.FormMethod 枚举中指定 HTTP 请求的参数,因此它使用默认的 POST 请求,就像 @Html.BeginForm() 一样,因此如果不妨碍 AJAX,它也会为默认表单方法写入 method="post"脚本在客户端被禁用。
data-ajax-method 属性的目的是在启用非侵入式 AJAX 时覆盖默认提交请求行为,因为它的值由 AjaxOptions.HttpMethod 属性设置,并由非侵入式 AJAX 库中的 asyncRequest() 方法检查(参见脚本的完整版本@ 987654326@):
function asyncRequest(element, options) {
var confirm, loading, method, duration;
confirm = element.getAttribute("data-ajax-confirm");
if (confirm && !window.confirm(confirm)) {
return;
}
loading = $(element.getAttribute("data-ajax-loading"));
duration = parseInt(element.getAttribute("data-ajax-loading-duration"), 10) || 0;
$.extend(options, {
type: element.getAttribute("data-ajax-method") || undefined, // here AJAX method is checked (GET or POST)
url: element.getAttribute("data-ajax-url") || undefined,
cache: (element.getAttribute("data-ajax-cache") || "").toLowerCase() === "true",
beforeSend: function (xhr) {
var result;
asyncOnBeforeSend(xhr, method);
result = getFunction(element.getAttribute("data-ajax-begin"), ["xhr"]).apply(element, arguments);
if (result !== false) {
loading.show(duration);
}
return result;
},
complete: function () {
loading.hide(duration);
getFunction(element.getAttribute("data-ajax-complete"), ["xhr", "status"]).apply(element, arguments);
},
success: function (data, status, xhr) {
asyncOnSuccess(element, data, xhr.getResponseHeader("Content-Type") || "text/html");
getFunction(element.getAttribute("data-ajax-success"), ["data", "status", "xhr"]).apply(element, arguments);
},
error: function () {
getFunction(element.getAttribute("data-ajax-failure"), ["xhr", "status", "error"]).apply(element, arguments);
}
});
注意:您可以在this reference中看到与AjaxOptions的每个属性对应的属性列表。