最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Issue with Knockout, Cannot use 'in' operator to search for 'length' - Stack Overfl

programmeradmin1浏览0评论

I am getting the following error on my web application using Knockout.js

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

My Code:

$(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>

And this is my model

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

And this is the code of the web service

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));

Any suggestions on what to do? I have searched everywhere on the web and can't find anything useful

Update 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

I am getting the following error on my web application using Knockout.js

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

My Code:

$(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>

And this is my model

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

And this is the code of the web service

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));

Any suggestions on what to do? I have searched everywhere on the web and can't find anything useful

Update 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
Share Improve this question edited Jul 5, 2012 at 12:11 Armand asked Jul 5, 2012 at 12:01 ArmandArmand 10.3k9 gold badges45 silver badges77 bronze badges 4
  • Are you getting that error in javascript? If so, your browsers debugger would point out the line where the error is happening. Can you post the code where the error is happening? – Amith George Commented Jul 5, 2012 at 12:05
  • Post the full error message, line number and file – Esailija Commented Jul 5, 2012 at 12:08
  • I have updated my question. The error is happening in the Knockout.js file – Armand Commented Jul 5, 2012 at 12:12
  • Try just: ko.applyBindings(viewModel); – Brian Mains Commented Jul 5, 2012 at 12:25
Add a ment  | 

1 Answer 1

Reset to default 5

I found my problem:

disabilities: ko.observableArray(data)

This piece of code should have been

disabilities: ko.observableArray(ko.utils.parseJson(data))

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论