【发布时间】:2017-10-30 12:29:46
【问题描述】:
我从数据库发送信息没有任何问题。但我无法在页面中加载表格。但是,当我发出警报以查看我收到的内容时,信息似乎以 json 的形式出现,但它继续在图片中给出错误的图像。我该如何解决?
我的 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;
}
【问题讨论】:
标签: javascript jquery ajax knockout.js