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