Javascript 如何将ajax调用中的数据附加到ul中的div

Javascript 如何将ajax调用中的数据附加到ul中的div,javascript,jquery,ajax,asp.net-core,asp.net-ajax,Javascript,Jquery,Ajax,Asp.net Core,Asp.net Ajax,我试图在同一个页面中从ajax检索数据到一个div,我未能从ajax获取数据,可能是我想要在其中获取数据的div,但是它是li的子对象,li是ul的子对象, 那我怎么解决呢 下面是HTML代码 <li> <ul class="notificationsbtn nav navbar-nav navbar-right"> <li id="notificationsli"> &l

我试图在同一个页面中从ajax检索数据到一个div,我未能从ajax获取数据,可能是我想要在其中获取数据的div,但是它是li的子对象,li是ul的子对象, 那我怎么解决呢

下面是HTML代码

<li>
    <ul class="notificationsbtn nav navbar-nav navbar-right">
        <li id="notificationsli">
            <a id="test" name="@currentUser.Id" href="#" role="button" class="dropdown-toggle" data-toggle="dropdown">
                <small><i class="glyphicon glyphicon-bell"></i></small>
                @*@if (@Model.Where(n => n.IsRead == false).Any())*@
                @*{*@<span class="noty-manager-bubble" style="margin-left:0px; top: 10px; opacity: 1;">@*@Model.Where(n => n.IsRead == false).Count()*@10</span>@*}*@
            </a>
            <div id="notification-container" class="dropdown-menu" role="menu" aria-labelledby="drop3">
                <section class="panel">
                    <header class="panel-heading">
                        <strong>The Notification</strong>
                    </header>
                    <div id="notification-list">
                        @*@await Component.InvokeAsync("Notification")*@
                        <div style="float:left;margin-top:20px" class="loader"></div>
                    </div>
                    <footer class="panel-footer">
                        <a href="#" class="pull-right"><i class="fa fa-cog"></i></a>
                        <a asp-action="Index" asp-controller="Notifications" asp-route-id="@currentUser.Id" class="text-success h5" data-toggle="class:show animated fadeInRight"><span style="margin-left:0px; top: 5px; opacity: 1;" class="glyphicon glyphicon-bell"></span> all notifications</a>
                    </footer>
                </section>

            </div>
        </li>
    </ul>
</li>

我遇到了一个问题,因为id(“通知列表”),ajax函数没有将数据附加到指定的div,当我将id更改为(“testn”)时,我得到了数据

 <script>
            $(document).ready(function () {
                $('#test').click(function () {
                {
                    //$("#notification-list").empty();
                    var _url = '@Url.Action("GetMyViewCompNotification", "Home")';
                    $(".loader").fadeIn();
                    $.ajax({
                        type: "GET",
                        url: _url,
                        data: { uid: $(this).prop("name") },
                        success: function (result) {
                            $(".testn").html(result);
                        },
                        error: function (xhr, status, err) {
                            alert(err.toString(), 'Error - LoadListItemsHelper');
                        },
                        complete: function (result, jqXHR, status) {
                            $(".loader").fadeOut();
                        }
                    });
                }
            });
        });
        </script>

$(文档).ready(函数(){
$(“#测试”)。单击(函数(){
{
//$(“#通知列表”).empty();
var_url='@url.Action(“GetMyViewCompNotification”,“Home”);
$(“.loader”).fadeIn();
$.ajax({
键入:“获取”,
url:_url,
数据:{uid:$(this.prop(“name”)},
成功:功能(结果){
$(“.testn”).html(结果);
},
错误:函数(xhr、状态、错误){
警报(err.toString(),'Error-LoadListItemsHelper');
},
完成:功能(结果、jqXHR、状态){
$(“.loader”).fadeOut();
}
});
}
});
});

我遇到了问题,由于id(“通知列表”)的原因,ajax函数没有将数据附加到指定的div,当我将id更改为(“testn”)时,我得到了数据

 <script>
            $(document).ready(function () {
                $('#test').click(function () {
                {
                    //$("#notification-list").empty();
                    var _url = '@Url.Action("GetMyViewCompNotification", "Home")';
                    $(".loader").fadeIn();
                    $.ajax({
                        type: "GET",
                        url: _url,
                        data: { uid: $(this).prop("name") },
                        success: function (result) {
                            $(".testn").html(result);
                        },
                        error: function (xhr, status, err) {
                            alert(err.toString(), 'Error - LoadListItemsHelper');
                        },
                        complete: function (result, jqXHR, status) {
                            $(".loader").fadeOut();
                        }
                    });
                }
            });
        });
        </script>

$(文档).ready(函数(){
$(“#测试”)。单击(函数(){
{
//$(“#通知列表”).empty();
var_url='@url.Action(“GetMyViewCompNotification”,“Home”);
$(“.loader”).fadeIn();
$.ajax({
键入:“获取”,
url:_url,
数据:{uid:$(this.prop(“name”)},
成功:功能(结果){
$(“.testn”).html(结果);
},
错误:函数(xhr、状态、错误){
警报(err.toString(),'Error-LoadListItemsHelper');
},
完成:功能(结果、jqXHR、状态){
$(“.loader”).fadeOut();
}
});
}
});
});

您能否在操作返回HTML中发送mvc操作代码?或jsonpublic IActionResult GetMyViewCompNotification(字符串uid){返回ViewComponent(“通知”,新的{id=uid});//它将调用Follower.cs InvokeAsync,并将id传递给它。}它返回视图组件,因此您在这里返回视图并在
通知列表中分配它是否正确?只需更改此
$(“#通知列表”).html(结果)
$(“#通知列表”)。追加(结果)你能发送mvc动作代码返回HTML吗?或jsonpublic IActionResult GetMyViewCompNotification(字符串uid){返回ViewComponent(“通知”,新的{id=uid});//它将调用Follower.cs InvokeAsync,并将id传递给它。}它返回视图组件,因此您在这里返回视图并在
通知列表中分配它是否正确?只需更改此
$(“#通知列表”).html(结果)
$(“#通知列表”)。追加(结果)
 <script>
            $(document).ready(function () {
                $('#test').click(function () {
                {
                    //$("#notification-list").empty();
                    var _url = '@Url.Action("GetMyViewCompNotification", "Home")';
                    $(".loader").fadeIn();
                    $.ajax({
                        type: "GET",
                        url: _url,
                        data: { uid: $(this).prop("name") },
                        success: function (result) {
                            $(".testn").html(result);
                        },
                        error: function (xhr, status, err) {
                            alert(err.toString(), 'Error - LoadListItemsHelper');
                        },
                        complete: function (result, jqXHR, status) {
                            $(".loader").fadeOut();
                        }
                    });
                }
            });
        });
        </script>