2017-05-30 91 views
-1

我從數據庫發送信息沒有任何問題。但我無法加載頁面中的表格。但是,當我警惕地看到我收到的信息時,這些信息似乎是以json的形式出現的,但它仍然在圖片中給出了錯誤的圖像。我如何解決它?Javascript Uncaught ReferenceError - C#

Image is here with error and alert.

我的HTML:

<!DOCTYPE html> 
<script src="Scripts/knockout-3.4.2.js" type="text/javascript"></script> 
<script src="Scripts/jquery-3.1.1.min.js"></script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <table border="1"> 
     <thead> 
      <th>Id</th> 
      <th>Name</th> 
      <th>Sales</th> 
      <th>Price</th> 
      <th>Sil</th> 
     </thead> 
     <tbody data-bind="foreach:friends"> 
      <tr> 
       <td data-bind="text:id"></td> 
       <td data-bind="text:name"></td> 
       <td data-bind="text:sales"></td> 
       <td data-bind="text:price"></td> 
       <td><input type="button" data-bind="click:$parent.removeUser" value="X" /></td> 
      </tr> 
     </tbody> 
    </table> 

    <div>Name</div> <input data-bind="value: Name" /> <br /> 
    <div>Sales </div> <input data-bind="value: Sales" /> <br /> 
    <div>Price </div> <input data-bind="value: Price" /> <br /> 

    <button data-bind="click:addUser">Ekle</button> 
    <button data-bind="click:removeUserAll">Hepsini Sil</button> 

    <script type="text/javascript"> 

     this.Name = ko.observable(); 
     this.Sales = ko.observable(); 
     this.Price = ko.observable(); 

     function functionViewModel() { 
      $(document).ready(function() { 
       $.ajax({ 
        type: "POST", 
        url: "KnockoutGrid2.aspx/GonderUrunler", 
        data: "{}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (msg) { 
         alert(msg.d); 
         var initialData = msg.d; 
        } 
       }); 
      }); 
      var fn = { 
       friends: ko.observableArray(initialData) 
      }; 

      return fn; 
     }; 

     ko.applyBindings(functionViewModel()); 
    </script> 
</body> 
</html> 

我的後端代碼:

[WebMethod] 
     public static string GonderUrunler() 
     { 
      denemeDBEntities db = new denemeDBEntities(); 
      var result = from d in db.urunler.ToList() 
         select d; 
      string output = new JavaScriptSerializer().Serialize(result); 
      return output; 
     } 

回答

0

您必須使用與ajax的回調。因爲ajax工作異步 檢查This文件。

$(document).ready(function() { 

     function functionViewModel(callback) { 
      $.ajax({ 
       type: "POST", 
       url: "KnockoutGrid2.aspx/GonderUrunler", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (msg) { 
        callback({friends: ko.observableArray(msg.d)}); 
       } 
      }); 
    }; 

    functionViewModel(function(response){ 
     ko.applyBindings(response.friends) 
    }); 
}); 
0

你知道,如果真的設定的 '朋友',是一個數組?你在你的foreach模板中使用它。它接縫敲除無法加載陣列...

<tbody data-bind="foreach:friends"> 
+0

如果我寫這樣的foreach工作,但這次我沒有數據庫中的數據。 var fn = { 朋友:ko.observableArray([{「id」:1,「name」:「Well-Traveled Kitten」,「sales」:「352」,「price」:「75.95」}, 「id」:2,「name」:「Speedy Coyote」,「sales」:「89」,「price」:「190.00」....}]) }; –

0

你返回從一個的WebMethod JSON格式的字符串,所以在您的JS代碼,你需要deserialise是一個對象,所以你可以用工作其中包含的數據。要做到這一點使用JSON.parse

success: function (msg) { 
    var data = JSON.parse(msg); 
    console.log(data[0].id); 
    console.log(data[0].name); 
} 

這裏有幾件事要注意。首先,data將是一個數組,因此如果您需要檢索其中包含的所有信息,則需要使用循環。其次,在調試時總是使用console.logconsole.diralert()將強制數據類型,所以它根本不可靠。

相關問題