这使我困惑。它一定是我看不到的小东西。我正在尝试使用ajax调用加载非常简单的observableArray
淘汰赛。从.ajax()调用knockout.js observableArray()调用
的JavaScript
// we bind the array to the view model property with an empty array.
var data = [];
var viewModel = {
vendors: ko.observableArray(data)
};
ko.applyBindings(viewModel);
$(function() {
// on this click event, we popular the observable array
$('#load').click(function() {
// WORKS. Html is updated appropriately.
viewModel.vendors([{ "Id": "01" },{ "Id": "02" },{ "Id": "03" }]);
// DOES NOT WORK. Fiddler2 shows the same exact json string come back
// as in the example above, and the success function is being called.
$.ajax({
url: '/vendors/10',
dataType: 'json',
success: function (data) {
viewModel.vendors(data);
}
});
});
});
HTML
<button id="load">Load</button>
<ul data-bind="template: { foreach: vendors }">
<li><span data-bind="text: Id"></span></li>
</ul>
问:为什么成功的Ajax调用,谁的data
变量值相匹配的字节为字节硬盘键入的值,不会触发h tml刷新?
感谢验证我的理智......我再仔细一点。也许MVC吸一点包装或什么... – 2012-03-16 05:51:09
叶。 Firebug请求响应,验证您正在获取json,然后验证jquery是否将其解析为对象。 – madcapnmckay 2012-03-16 05:55:34
它是肯定的json ... C#代码看起来像'return Json(list,JsonResponseBehavior.AllowGet);'其中'list'是一个ICollection所以我知道它是JSON。除此之外,Fiddler2可以正确显示其JSON视图中的数据......它在JavaScript的某处。当我在工作时,我会在明天再次发布更多信息 – 2012-03-16 05:58:07