【问题标题】:How to get data from json in lightgallery jquery?如何从lightgallery jquery中的json获取数据?
【发布时间】:2017-10-19 19:16:01
【问题描述】:

我在使用 lightgalleryjquery 时遇到问题。当我对 src 和 thumb 使用常量值时,它工作得很好,但相反,它不适用于 json
我在firebug 中看到了这种错误。

错误

幻灯片项目上没有提供-data-src


HTML 代码

<a id="dynamic"  href>Open lightGallery</a>

$('#dynamic').on('click', function(e) { 

            var arr=''; 
            $.ajax({ 
                type: 'post', 
                url: "@Url.Action("ShowMenuGallery", "Ads")", 
                contentType: 'application/json; charset=utf-8', 
                data: '{"ID":"' + @Model.ID + '"}', 
                traditional: true, 
                success: function (data) { 

                    arr=data; 
                    console.log(arr); 
                } 

            }); 


            $('#dynamic').lightGallery({ 
                dynamic: true, 
                html:true, 
                dynamicEl:  JSON.stringify(arr) 
            }) 

        });

Controller.cs

public ActionResult ShowMenuGallery(int id) 
        { 
            ViewBag.guid = Guid.NewGuid(); 
            List<string> menuImage = new List<string>(); 
            ReturnImages(((Guid)ViewBag.guid).ToString(), id, ref menuImage); 



        List<MyClass> data = new List<MyClass>() { new MyClass() { src =menuImage[0] , thumb = menuImage[0] } }; 
        var json = Newtonsoft.Json.JsonConvert.SerializeObject(new 
        { 
            operations = data 
        }); 
            return Json(json); 

    } 
public class MyClass 
        { 
            public string src { get; set; } 
            public string thumb { get; set; } 
        }

【问题讨论】:

    标签: javascript jquery json asp.net-mvc


    【解决方案1】:

    试试这段代码

    jQuery(document).ready(function($) {
        $("#dynamic").on("click", function(e) { 
            $.ajax({ 
                type: "post", 
                url: "@Url.Action("ShowMenuGallery", "Ads")", 
                dataType: "json", 
                data: {ID: "@Model.ID"}, 
                traditional: true, 
                success: function (data) { 
                    $("#dynamic").lightGallery({ 
                        dynamic: true, 
                        html:true, 
                        dynamicEl:  JSON.stringify(data) 
                    });
                } 
            }); 
        });
    });
    

    欲了解更多信息,请参阅https://boxoverflow.com/get-json-jquery-ajax/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2013-02-19
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多