我正在努力返回 JSON 数据并将其转换为 observable。数据以 JSON 格式返回,但似乎没有分配给 observable。有人可以帮忙吗?我猜问题出在 ajax 调用的成功部分:
<script type="text/javascript">
function StandingsViewModel() {
var self = this;
self.standings = ko.observableArray();
self.DivisionName = ko.observable('');
self.afceast = ko.computed(function () {
return ko.utils.arrayFilter(self.standings(), function (i) {
return "AFC East" == i.DivisionName;
});
});
self.afccentral = ko.computed(function () {
return ko.utils.arrayFilter(self.standings(), function (i) {
return "AFC Central" == i.DivisionName;
});
});
self.afcwest = ko.computed(function () {
return ko.utils.arrayFilter(self.standings(), function (i) {
return "AFC West" == i.DivisionName;
});
});
self.nfceast = ko.computed(function () {
return ko.utils.arrayFilter(self.standings(), function (i) {
return "NFC East" == i.DivisionName;
});
});
self.nfccentral = ko.computed(function () {
return ko.utils.arrayFilter(self.standings(), function (i) {
return "NFC Central" == i.DivisionName;
});
});
self.nfcwest = ko.computed(function () {
return ko.utils.arrayFilter(self.standings(), function (i) {
return "NFC West" == i.DivisionName;
});
});
$.ajax({
dataType: "json",
url: "/api/standing/GetStandingsBySeason/2018",
beforeSend: function (xhr) {
$('#divStandings').html('');
$('#divStandings').addClass('ajaxRefreshing');
xhr.setRequestHeader('X-Client', 'jQuery');
},
success: function (result) {
$('#divStandings').removeClass('ajaxRefreshing');
self.standings(JSON.parse(result));
}
});
}
$(document).ready(function () {
ko.applyBindings(new StandingsViewModel());
});
</script>