1.i have 2 question about knockout on asp.net.
server side method:
<WebMethod>
Public Shared Function foo() As List(Of person)
Return New List(Of person)() From {New person With {.id = 1, .fname = "meysam"},
New person With {.id = 2, .fname = "yasaman"}}.ToList
End Function
json and binding to knockout:
var VM = {};
$.getJSON("default.aspx/foo", function (data) {
var tmp = JSON.stringify(data.d);
VM.model =
ko.applyBindings(VM);
});
html:
<div data-bind="foreach: model">
<h3 data-bind="text: id">
</h3>
<p>
Name: <span data-bind="text: fname"></span>
</p>
but when i run in browser.No nothing will happen.fname,id Will be displayed.
2.what The difference between $.getJSONand dataType:json?
$.getJSON("default.aspx/foo", function (data) {
var tmp = JSON.stringify(data.d);
});
and
$.ajax({
type:"post",
url:"default.aspx/foo",
contentType:"application/json;charset=utf-8",
dataType:"json",
success:function(data){}
})
About your knockout question:
Here’s code to solve your issue:
Output
ASPX code behind
ASPX
.getJSONis a shortcut, and it internally uses.ajaxThe
.getJSONfunction sets the following properties:dataType: “json”type: "GET"