javascript - Ajax 不与 owl-carousel 一起使用

标签 javascript jquery ajax asp.net-mvc

我在我的页面中实现 ajax 时遇到问题,其中的代码是 owl-carousel:

Controller :

 public int Getmembers()
    {

        return db.Teams.Count();

        }
    }

View :

<script>
$(document).ready(function () {
                        var json = null;
                        $.ajax({
                            url: '@Url.Action("Getmembers", "Home")',
                            type: "GET",
                            dataType: "int",
                            success: function (data) {
                                json = data;

                            },
                            error: function () {
                                alert("Erreur de récupération des membres ou il n'existe aucun memnbre");
                            }

                        })
                        alert(json);
                        var owl = $("#owl-demo");

                        owl.owlCarousel({
                            items: json, //10 items above 1000px browser width
                            itemsDesktop: [1000, 5], //5 items between 1000px and 901px
                            itemsDesktopSmall: [900, 3], // betweem 900px and 601px
                            itemsTablet: [600, 2], //2 items between 600 and 0
                            itemsMobile: false, // itemsMobile disabled - inherit from itemsTablet option
                            pagination: false

                        });


                    });
</script>

我尝试调试,发现在 $.ajax 中传递了 json 的值(int 13),但是当我使用警报时,我得到 null ,我在文档中搜索得到了这个 http://www.jq22.com/demo/OwlCarousel2/demos/ajax.html但仍然没有任何线索。

最佳答案

您的 ajax 调用是异步的,这意味着它将在 owl carousel 初始化后执行 success 方法。

将轮播的 init 方法放入 success 方法中:

    $(document).ready(function () {
    var json = null;
    $.ajax({
        url: '@Url.Action("Getmembers", "Home")',
        type: "GET",
        dataType: "int",
        success: function (data) {
            var owl = $("#owl-demo");

            owl.owlCarousel({
                items: data, //10 items above 1000px browser width
                itemsDesktop: [1000, 5], //5 items between 1000px and 901px
                itemsDesktopSmall: [900, 3], // betweem 900px and 601px
                itemsTablet: [600, 2], //2 items between 600 and 0
                itemsMobile: false, // itemsMobile disabled - inherit from itemsTablet option
                pagination: false

            });

        },
        error: function () {
            alert("Erreur de récupération des membres ou il n'existe aucun memnbre");
        }

    });
});

关于javascript - Ajax 不与 owl-carousel 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692058/

相关文章:

javascript - 多个弹出窗口 - 超出最大调用堆栈大小 - jQuery 错误

javascript - 验证日期选择器不输入旧日期

javascript - 使用 Javascript 翻转添加类

javascript - "Waiting"表示 ReadyState 在返回值之前为 4

javascript - 无法将 nest.js 项目部署到 Google Firebase Functions

javascript - 如何在 Laravel 中通过 Sweet-alert 确认后删除...?

javascript - 无法到达 JavaScript 中的返回函数

javascript - 从下拉列表中实时预览 AJAX 函数

javascript - IE 11 中的 url 编码

javascript - jquery验证后的ajax请求