我正在尝试使用 JQuery Ajax 在容器 div 中渲染部分 View 。 这是我的 ajax 调用:
var href = {
href: $(this).attr("href").substr(1)
}
var container = $(".customer-main-content-container");
$.ajax({
url: "@Url.Action(" CustomerMenuSelection ")",
type: "POST",
dataType: "html",
contentType: "application/json",
data: JSON.stringify(href),
error: function (jqXHR, textStatus, errorThrown) {
alert("ERROR: " + errorThrown.text());
},
success: function (result) {
container.empty();
container.html(result).show();
}
});
更新
这是我的操作代码:
public ActionResult CustomerMenuSelection(string href)
{
var user = GetCurrentUser();
var tempSensorTree = _prtgClient.GetPrtgSensorTree(user.TempPrtgGroupId.ToString());
var tempDevices = tempSensorTree.Sensortree.Nodes.Group.Device;
return PartialView("_Monitor", tempDevices);
}
我通过我的操作跟踪了调用,发现它确实将所有正确的数据发送回 View 。但是,我的 ajax 调用没有运行错误或成功回调,我不知道为什么。单击菜单项时会发生这种情况,并且相同的 ajax 调用适用于除此菜单项之外的所有其他菜单项。我找不到页面之间的任何差异。 没有抛出错误,我填充 View 的数据是正确的。我的 ajax 调用停止了。
简而言之,为什么我的回调没有被触发?
感谢您的帮助! 提前致谢 马丁·约翰逊
最佳答案
出现此问题的原因可能是您期望服务器提供的 dataType
。当您从服务器返回部分 View 时,请尝试将其更改为 "html"
。
dataType: "html"
关于JQuery Ajax,ASP.NET MVC 5 中没有运行回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30703264/