JQuery Ajax,没有回调函数在ASP.NET MVC 5中运行

时间:2022-12-08 19:29:23

I'm trying to render a partial view in a container div using JQuery Ajax. Here's my ajax-call:

我正在尝试使用JQuery Ajax在容器div中呈现局部视图。这是我的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();

    }
});

Update

Here's my Action code:

这是我的行动代码:

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);
}

I've followed the call through my Action and found that it indeed sends all the correct data back to the view. However, my ajax-call is not running either error- or success-callback and I have no idea why. This is happening when clicking a menu-item, and this same ajax-call works for all other menu-items except this one. I can't find any differences between the pages. No errors are thrown, the data that I populate the view with is correct. My ajax-call just stops.

我通过我的Action跟踪了调用,发现它确实将所有正确的数据发送回视图。但是,我的ajax调用没有运行错误或成功回调,我不知道为什么。单击菜单项时会发生这种情况,同样的ajax-call适用于除此之外的所有其他菜单项。我找不到页面之间的任何差异。没有抛出错误,我填充视图的数据是正确的。我的ajax电话刚刚停止。

So in short, why is my callbacks not triggered?

简而言之,为什么我的回调没有被触发?

Grateful for any assistance! Thanks in advance Martin Johansson

感谢任何帮助!在此先感谢Martin Johansson

1 个解决方案

#1


1  

This issue might be occurring because of dataType you are expecting from server. Try changing it to "html" as you are returning partial view from server.

由于您期望从服务器获取dataType,可能会发生此问题。当您从服务器返回部分视图时,尝试将其更改为“html”。

dataType: "html"

#1


1  

This issue might be occurring because of dataType you are expecting from server. Try changing it to "html" as you are returning partial view from server.

由于您期望从服务器获取dataType,可能会发生此问题。当您从服务器返回部分视图时,尝试将其更改为“html”。

dataType: "html"