使用标签data- *实现Ajax调用

问题描述 投票:15回答:1

我正在尝试使用jquery-ajax-unobtrusive中描述的标签data- *来实现Ajax调用

我知道我可以使用$ .ajax()的javascript函数,但我的目的是试验标签data-ajax ...

我在cshtml文件中尝试了这段代码

<a class="btn btn-primary" data-ajax-url="/Home/AjaxSample" data-ajax="true" data-ajax-success="AjaxSuccess" data-ajax-failure="AjaxError" data-ajax-method="GET">Ajax</a>

// other code...

function AjaxSuccess() { alert('AjaxSuccess'); }
function AjaxError() { alert('AjaxError'); }

浏览器当然会显示按钮,但单击它时没有任何反应。我无法在浏览器调试器中发现任何错误。显然没有任何事情发生。

Intellisense不显示标签data-ajax和friends。我忘了包含一些东西吗?

有没有完整的工作实例?

javascript ajax asp.net-mvc asp.net-core
1个回答
1
投票

评论的答案(因为没有人添加它):

<script src="~/lib/jquery-ajax-unobtrusive/jquery.unobtrusive-ajax.j‌​s"></script>
<!-- the ~ is to symbolize your project's wwwroot folder

使用所有其他脚本(默认情况下为_Layout.cshtml)将上面的内容添加到主布局中。

这样做包括将jQuery不显眼的ajax文件包含到所有页面中,以便包含完成所有工作的主脚本。

在此之前,请确保已安装包Microsoft.Jquery.Unobtrusive.Ajax

© www.soinside.com 2019 - 2024. All rights reserved.