【发布时间】:2021-01-31 23:42:48
【问题描述】:
我正在尝试在我的 Razor 页面中进行 ajax 调用。截至目前,我的控制器方法应该返回字符串“ajax return”,然后将其呈现给适当的 div 标签。这只是一个测试。我无法让 ajax 调用在后面的代码中点击我的方法。提前感谢您的任何意见!
ajax:
function test() {
$.ajax({
type: "GET",
url: "@Url.Action("AjaxThing", "Posts")";
data: "Andrew",
success: function (response) {
$("#test").html(response.data);
}
});
}
$("#clickMe").on("click", test);
cshtml:
<div id="test">
</div>
<div class="form-group">
<button id="clickMe" class="btn">Click me</button>
</div>
后面的代码:
public string AjaxThing()
{
return "ajax return";
}
解决方案布局:
【问题讨论】:
-
嗯……你根本没有调用
test函数…… -
天哪,我在帖子中意外留下了一行。现在更新了。
-
您的开发者工具日志中写了什么?
-
url: "@Url.Action("AjaxThing", "Posts")";- 这个语法看起来不正确,正如您在突出显示时看到的那样。也许应该是url: @Url.Action("AjaxThing", "Posts"); -
@Xerillio 我刚试过,没有效果。
标签: c# ajax asp.net-mvc razor-pages