简体   繁体   English

未捕获的TypeError:无法读取未定义的属性'fromJSON'

[英]Uncaught TypeError: Cannot read property 'fromJSON' of undefined

I am trying to display menu data after the ajax call returns from the following page: 我试图在ajax调用从以下页面返回后显示菜单数据:

<head>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">

    <script type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
    <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
    <!-- <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js'></script>
    <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.js'></script> -->
</head>


<body>

    <h1>page loaded</h1>

    <div data-bind="foreach: menu">
        <!-- ko foreach: items -->
            <span data-bind="$data.name"></span>
        <!-- /ko -->
    </div>
</body>




<script>

    function OrderPageViewModel() {
        var self = this;

        // self.menu_loaded_success_callback = function(data) {
        //         var retrieved_menu = data["menu"];
        //         self.menu = ko.observable(retrieved_menu);
        //         console.log(retrieved_menu)
        //         alert(data);
        //     }
        //
        // self.get_data(menu_url).done(self.menu_loaded_success_callback);

    }

    var menu_url = "menu/get-menu";

    get_menu_data = function(url_ending){
        console.log("running get_data");
        var URL = "/api/&/".replace("&", url_ending);
        console.log("URL: ", URL);
        return $.ajax({
            dataType: "json",
            url: URL,
            // type: "GET",
        });
    }

    get_menu_data(menu_url).done(function(data){
        var vm = new OrderPageViewModel();
        console.log(data);
        vm = ko.mapping.fromJSON(data);
        ko.applyBindings(vm);
    }).fail(function(){
        alert("failed");
    });

    // ko.applyBindings(new OrderPageViewModel());

</script>

在此处输入图片说明

I followed the example at wait for ajax result to bind knockout model and it is saying Uncaught TypeError: Cannot read property 'fromJSON' of undefined 我在等待ajax结果绑定敲除模型时遵循了示例,并说Uncaught TypeError: Cannot read property 'fromJSON' of undefined

The fromJSON method is part of the Knockout mapping plugin which is not included in the code Knockout library. fromJSON方法是Knockout映射插件的一部分,该插件未包含在代码Knockout库中。

You have to include the mapping plugin separately, so add the following line after you have included knockout: 您必须单独包含映射插件,因此在添加了剔除后添加以下行:

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js" type='text/javascript'></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 未捕获的TypeError:无法读取未定义的属性“数量” - Uncaught TypeError: Cannot read property 'quantity' of undefined 未捕获的TypeError:无法读取未定义的属性“ timing” - Uncaught TypeError: Cannot read property 'timing' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;formatter&#39; - Uncaught TypeError: Cannot read property 'formatter' of undefined Uncaught TypeError:无法读取未定义的属性“ stopVideo” - Uncaught TypeError: Cannot read property 'stopVideo' of undefined 未捕获的类型错误:无法读取未定义的属性“setCrossOrigin” - Uncaught TypeError: Cannot read property 'setCrossOrigin' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;NaN&#39; - Uncaught TypeError: Cannot read property 'NaN' of undefined 未捕获的TypeError:无法读取未定义的属性&#39;getAttribute&#39; - Uncaught TypeError: Cannot read property 'getAttribute' of undefined 未捕获的TypeError:无法读取未定义的属性“地理编码” - Uncaught TypeError: Cannot read property 'geocode' of undefined 未捕获的TypeError:无法读取未定义的属性“时间戳” - Uncaught TypeError: Cannot read property 'timestamp' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM