我有一个 F# mvc 应用程序,我试图让 ajax 调用和端点返回一个人员列表,然后显示在一个表格中。
type Person(credits:int, name:string, serverDateTime:DateTime ) = class end
type PeopleMeta( persons:ResizeArray<Person> ) =
member t.size() = 1
[<HttpGet>]
member this.getPeopleMeta()=
let person1 = Person(1,"A",DateTime.Now)
let person2 = Person(2,"B",DateTime.Now)
let persons = new ResizeArray<Person>()
persons.Add(person1);
persons.Add(person2);
let peopleMeta = PeopleMeta( persons )
this.Json( peopleMeta , JsonRequestBehavior.AllowGet )
在 Html/JavaScript 我有这个...
<script src="/knockout-2.2.0.js"></script>
<script src="knockout.mapping.js"></script>
<body>
<div id="contentViewModelAjax">
<div>
<div data-bind="template: { name: 'personAjax-template', foreach: people }">
</div>
</div>
</div>
<script type="text/html" id="personAjax-template">
<span data-bind="text: name"></span>
<span data-bind="text: credits"></span>
<span data-bind="text: serverDateTime"></span>
<br />
</script>
<script>
$(document).ready(function () {
execPageViewModel();
});
var execPageViewModel = function () {
function PeopleViewModel(config) {
var self = this;
self.config = config;
self.people = ko.observableArray([]);
self.getPeople = function () {
var url = "getPeopleMeta";
var jqXhr = $.ajax({ url: url, type: 'GET', cache: false, 'data':self.config });
jqXhr.done(function (data) {
console.log(data);
console.log(data.persons);
self.people(data.persons);
ko.applyBindings(self, document.getElementById(self.elementId));
});
setTimeout(self.getPeople, 5000);
};
self.exec = function () {
self.getPeople();
};
};
var viewModelConfig = {'elementId':'contentViewModelAjax','PersonId':1};
var peopleViewModel = new PeopleViewModel(viewModelConfig);
peopleViewModel.exec();
};
</script>
console.log 数据为 {},data.persons 未定义。有任何想法吗 ?