简体   繁体   English

我得到一个JSON / Javascript对象是因为可以在控制台中看到它,但是如何获取它进行序列化并使数据绑定到Ui元素呢?

[英]I am getting a JSON/Javascript object because I can see it in my console, but how do I get it to serialize and make my data bind to my Ui elements?

I am using knockout. 我正在使用淘汰赛。 I am sending dynamic data from my MySql server to a wcf service and then getting my knockout function to display the data in divs. 我正在将动态数据从MySql服务器发送到wcf服务,然后获取敲除函数以divs显示数据。 I will post the code below so you can see what I'm talking about. 我将在下面发布代码,以便您可以了解我在说什么。

My knockout model: 我的淘汰赛模型:

function machineDataModel(item) {
    this.mach_no = ko.observable(item.mach_no),
    this.VAR1 = ko.observable(item.VAR1),
    this.VAR2 = ko.observable(item.VAR2),
    this.VAR3 = ko.observable(item.VAR3),
    this.VAR4 = ko.observable(item.VAR4)
};

Knockout ViewModel: 剔除ViewModel:

$(document).ready(function () {

    var machineDataViewModel = {

        machineDataItems: ko.observableArray([]),

        loadMachineDataItems: function () {
            alert('Method 1: Start');
            $.getJSON("http://localhost/JsonRestful/Service1.svc/GetMachineData", function (data) {
                alert(data);
                console.log(data);
                //machineDataViewModel.machineDataItems.removeAll();
                //alert('Everything Removed')
                alert('Method 2: Start');
                $.each(data, function (index, item) {
                    machineDataViewModel.machineDataItems.push(new machineDataModel(item));

                });
            });
        }
    };

    ko.applyBindings(machineDataViewModel);

    machineDataViewModel.loadMachineDataItems();

    //setInterval( loadMachineDataItems, 10000 );
});

I know everything works fine till I get to the alert Method 2: Start. 我知道一切正常,直到我收到警报。方法2:开始。 The each method doesn't iterate over the array like I want it to. 每个方法都不会像我想要的那样遍历数组。 I get an object in my console where I requested it be posted in the log. 我在控制台中收到一个对象,该对象要求将其发布到日志中。 Object { GetMachineDataResult=[20]}

My View is: 我的看法是:

<div id="knockout" data-bind="foreach: machineDataItems">
    <p id="machineNum" data-bind="text: mach_no"></p>
    <div id="Completed" data-bind="text: VAR1"></div>
    <div id="Style" data-bind="text: VAR2"></div>
    <div id="PUPC" data-bind="text: VAR3"></div>
    <div id="RPM" data-bind="text: VAR4"></div>
</div>

From the image of your JSON response you posted in a comment, it looks like your JSON object looks something like this: 从您在评论中发布的JSON响应的图像来看,您的JSON对象看起来像这样:

{
    "GetMachineDataResult": [ // bunch of items here ]
}

Note that the array isn't the top level object, it's a property called GetMachineDateResult . 注意,该数组不是顶级对象,而是一个名为GetMachineDateResult的属性。 So instead of: 所以代替:

$.each(data, function (index, item) { //...

You need: 你需要:

$.each(data.GetMachineDateResult, function (index, item) { //...

Because data is an object and data.getMachineDateResult is the array you want. 因为data是一个对象,而data.getMachineDateResult是您想要的数组。

暂无
暂无

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

相关问题 如何在禁用Javascript的情况下让我的UI“优雅地降级”? - How do I make my UI 'degrade gracefully' with Javascript disabled? 为什么我的控制台上未定义? - Why am I getting undefined on my console? 我正在尝试记录提交按钮表单的元素,但在控制台浏览器中看不到任何内容 - I am trying to log the elements of my submit buttons form but it I cannot see anything in the console browser 我的“t”在控制台中未定义,我不知道我的代码有什么问题。 如何在此代码中定义我的“t” - Am getting my "t" as undefined in the console and i do not know what is wrong with my code. How can I define my 't' in this code 当我试图确保我的 javascript 代码正常工作时,我在 Google Chrome 上的控制台上收到此错误 - I get this error on my console on Google Chrome when I am trying to make sure that my javascript code is working 如何在我的选择中从json获取对象数据? - How do I get object data from my json in my select? 如何在我的组件上看到我的数组? / 反应 / Javascript - How can i see my array on my component ? / React / Javascript 我可以将我的 HTML 元素的文本绑定到字符串的 object 吗? - Can I bind my HTML elements' text to an object of strings? 如何使用javascript在对象数组中使json对象的字段名称具有值? - How can I make my json object's field name a value with in an object array with javascript? 我如何处理我的json元素 - how can i handle my json elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM