2017-10-19 57 views
0

我面臨使用中的問題lightgalleryjquery。當我使用恆定值的src和拇指它很好,但在相反它不適用於json
我在firebug看到這種錯誤。

錯誤如何從jQuery中獲取lightgallery jquery中的數據?

不設置在滑動項-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; } 
     } 

回答

0

一次嘗試這個代碼

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/