Error in knockout mapping JSON read

2019-02-21 01:06发布

i am using knockout on web application which displays the data with a foreach (all needed libraries are included)

and i get the following error:

Error: Unable to parse bindings. Message: ReferenceError: d is not defined; Bindings value: foreach: d
[Break On This Error]   

...+c+" } ";return new Function("sc",c)},kb:function(a,b){if(b.compareDocumentPosit...

my JSON:

{"d":[{"__type":"listingItem:#applicationModel","award_text":"","channel_id":5,"constructed_short_descriptionen":"","constructed_short_descriptiongr":"","constructed_titleen":"","constructed_titlegr":"","country_id"...

My Code:

var Listing_ViewModel = {};

    var data = $.getJSON("listings.svc/Listing_Get_Items?",
                {
                    startdate: "2012-09-21 00:00:00",
                    stopdate: "2012-09-22 00:00:00",
                    channel_id: "5"
                }, function (data) {
                    // Now use this data to update your view models, 
                    // and Knockout will update your UI automatically 
                    //var parsed = JSON.parse(data);
                    alert(data.d[0].duration);
                    Listing_ViewModel = ko.mapping.fromJSON(data);
                    //alert(ko.mapping.toJS(Listing_ViewModel));
                    ko.applyBindings(Listing_ViewModel);
                    //        ko.applyBindings({
                    //          
                    //        });
                })

HTML code:

<!-- ko foreach: d -->
    <li class="item item-even" id="id_1" title="1">
    </li>
<!-- /ko -->

Any ideas what am i doing wrong? Sorry i am completely green!

1条回答
该账号已被封号
2楼-- · 2019-02-21 01:17

The problem is most likely that you are using fromJSON when you really mean fromJS. $.getJSON will already create an object for you while ko.mapping.fromJSON reads in a JSON string. ko.mapping.fromJS reads in a Javascript Object.

See this JSFiddle for more: http://jsfiddle.net/hB3Rm/

Essentially, change this line:

Listing_ViewModel = ko.mapping.fromJSON(data);

to this line:

Listing_ViewModel = ko.mapping.fromJS(data);
查看更多
登录 后发表回答