5

我在使用 Knockout.js 的 Web 应用程序上收到以下错误

Cannot use 'in' operator to search for 'length'

我的代码:

$(document).ready(function () {
        AjaxRequest();
    });

    function AjaxRequest() {
        $.post("../../Api/DisabilitiesHandler.ashx?method=get", function (data) {
            var viewModel = {
                disabilities: ko.observableArray(data)
            };

            ko.applyBindings( viewModel, document.body);
        });
    }

<table>
    <tbody data-bind="template: { name: 'disabilitiesRowTemplate', foreach: disabilities }"></tbody>
</table>

<script type="text/html" id="disabilitiesRowTemplate">
    <tr>
        <td>Name:
            <input data-bind="value: Name" /></td>
        <td>
           Active <input type="checkbox" data-bind="checked: Active" /></td>
    </tr>
</script>

这是我的模型

public class Disabilities
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Active { get; set; }
}

这是网络服务的代码

context.Response.ContentType = "application/JSON";
DbsaDal.Entities.DBSAEntities db = new DbsaDal.Entities.DBSAEntities();
List<DbsaDal.Model.Disabilities> disabilities = DbsaDal.Entities.Disabilities.Get(db);
context.Response.Write(new JavaScriptSerializer().Serialize(disabilities));

关于做什么的任何建议?我在网上到处搜索,找不到任何有用的东西

更新1:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"Id":1,"Name":"Blind","Active":false},{"Id":2,"Name":"Mute","Active":true}] Knockout.js:92
4

1 回答 1

5

我发现了我的问题:

disabilities: ko.observableArray(data)

这段代码应该是

disabilities: ko.observableArray(ko.utils.parseJson(data))
于 2012-07-05T12:28:44.820 回答