挖空映射JSON读取错误 [英] Error in knockout mapping JSON read

查看:134
本文介绍了挖空映射JSON读取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web应用程序上的剔除功能,该功能使用foreach显示数据 (包括所有需要的库)

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

我得到以下错误:

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...

我的JSON:

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

我的代码:

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代码:

<!-- 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!

推荐答案

这个问题很可能是您真正使用fromJS时正在使用fromJSON的原因. ko.mapping.fromJSON会为您创建一个对象,而ko.mapping.fromJSON会读取JSON字符串. ko.mapping.fromJS读取Javascript对象.

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.

有关更多信息,请参见此JSFiddle: http://jsfiddle.net/hB3Rm/

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

基本上,请更改此行:

Listing_ViewModel = ko.mapping.fromJSON(data);

此行:

Listing_ViewModel = ko.mapping.fromJS(data);

这篇关于挖空映射JSON读取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆