我是淘汰赛的新手。我正在尝试绑定从ajax调用返回的json,但它没有发生。
<script type="text/javascript">
var ServerData;
$(document).ready(function () {
ko.setTemplateEngine(new ko.nativeTemplateEngine);
var oViewModel = new CompanyModel();
oViewModel.Newitem(new Company("","","","","","","","","","","","","","",""));
ko.applyBindings(oViewModel);
BasicDatatable('#TableCompany');
});
function CompanyModel() {
var self = this;
self.CompanyList = ko.observable();
$.getJSON("getallcompanies", function (response) {
ServerData = ko.mapping.fromJSON(response);
self.CompanyList(ServerData);
});
}
在response
我有所需的数据。但是在ServerData
中,我得到以下内容而不是列表。
function b() { if (0 < arguments.length) { if (!b.equalityComparer || !b.equalityComparer(d, arguments[0])) b.H(), d = arguments[0], b.G(); return this } r.T.Ha(b); return d }
你能告诉我出了什么问题吗?
答案 0 :(得分:0)
您应该使用ko.mapping.fromJS
来绑定来自服务器的JSON。
此外,您不需要定义self.CompanyList = ko.observable();
并将设置值绑定到self.CompanyList(ServerData);
中的该属性。
这一切都由ko.mapping.fromJS
完成。来自Knockout Mapping文档:
尝试更改此代码:
function CompanyModel() {
var self = this;
$.getJSON("getallcompanies", function (response) {
ko.mapping.fromJSON(response, {}, self);
});
}