Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法将JSON(从WCFRest返回)绑定到HTML表_Javascript_Jquery_Html_Knockout.js_Wcf Rest - Fatal编程技术网

Javascript 无法将JSON(从WCFRest返回)绑定到HTML表

Javascript 无法将JSON(从WCFRest返回)绑定到HTML表,javascript,jquery,html,knockout.js,wcf-rest,Javascript,Jquery,Html,Knockout.js,Wcf Rest,我已经创建了一个wcfresvile服务,它以以下格式填充JSON数据 {"GetEmployeesJSONResult":"[{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"S

我已经创建了一个wcfresvile服务,它以以下格式填充
JSON
数据

{"GetEmployeesJSONResult":"[{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000},{\"Name\":\"Sumanth\",\"Id\":101,\"Salary\":5000}]"}
我试图使用Knockout在HTML表中显示这些数据,但到目前为止还没有实现。但是,硬编码值可以正确显示

HTML和敲除

var HomeModel=函数(){
this.rows=ko.observableArray();
};
$(文档).ready(函数(){
$.ajax({
方法:“张贴”,
网址:'http://localhost:1249/Service1.svc/GetJsonAll',
contentType:“应用程序/javascript”,
数据类型:“jsonp”,
成功:函数(数据){
//用于(数据中的var x){
//model.rows.push(数据[x]);
//}
//模型行(数据);
//console.log(model.rows);
//var res=[{
//“Id”:“1”,
//“姓名”:“迈克”,
//“开始日期”:“2008年6月1日太阳”,
//“完成日期”:“2008年6月1日太阳”,
//“工资”:“实习生”
//}, {
//“Id”:“2”,
//“姓名”:“Jhon”,
//“开始日期”:“2008年6月1日太阳”,
//“完成日期”:“2008年6月1日太阳”,
//“工资”:“实习生”
//}, {
//“Id”:“2”,
//“姓名”:“Jhon”,
//“开始日期”:“2008年6月1日太阳”,
//“完成日期”:“2008年6月1日太阳”,
//“工资”:“实习生”
//}];
控制台日志(数据);
var模型=新的HomeModel();
//ko.应用绑定(模型);
ko.applyBindings({
行:data.GetEmployeesJSONResult
});
}
});
});
员工ID
名称
薪水

谁能帮帮我吗?我被卡住了。

只是在黑暗中打了一枪,你试过推杆吗

var HomeModel=函数(){
this.rows=ko.observableArray();
};
$(文档).ready(函数(){};
)中? 是否尝试在值进入DOM之前获取它们?

检查

该代码>var数据<<代码>var数据={{,”,”,”,”,”var数据<<,”,”,”,”var数据<<<代码>var数据={{,”,”var数据={,”,”,”,”,”,”,”,”代码数据>var数据数据={{代码数据数据数据={”名名名名名名名名名名“:::“苏门坦斯”这笔这笔交易交易交易,”101,:101,:101,,:101,,,,,,,,,,“工资\“工资::::::5000名名::::::::::::::5000,,,,,,,,,,,,,,“工资\“工资\“工资\“工资\“工资\“工资:::::::::::::5000\“工资:101,”10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10,”工资:101,,,,,,“Sumanth\”、“Id\”:101、“Salary\”:5000}]“}” var HomeModel=函数(){ this.rows=ko.observableArray(); }; ko.applyBindings({ 行:JSON.parse(data.GetEmployeesJSONResult) });

员工ID
名称
薪水

是的。它被添加了。非常感谢您的关注。JSON.parse(data.GetEmployeesJSONResult)完成了这个把戏。非常感谢Rony!JSON.parse(data.GetEmployeesJSONResult)完成了这个把戏。您救了我一天。
var HomeModel = function () {
        this.rows = ko.observableArray();
    };

    $(document).ready(function () {            

        $.ajax({
            method: "POST",
            url: 'http://localhost:1249/Service1.svc/GetJsonAll',
            contentType: "application/javascript",
            dataType: "jsonp",
            success: function (data) {                    
                //for (var x in data) {
                //    model.rows.push(data[x]);
                //}
                //model.rows(data);
                //console.log(model.rows);
                //var res = [{
                //    "Id": "1",
                //    "Name": "Mike",
                //    "Start_Date": "Sun 01/06/08",
                //    "Finish_Date": "Sun 01/06/08",
                //    "Salary": "Trainee"

                //}, {
                //    "Id": "2",
                //    "Name": "Jhon",
                //    "Start_Date": "Sun 01/06/08",
                //    "Finish_Date": "Sun 01/06/08",
                //    "Salary": "Trainee"
                //}, {
                //    "Id": "2",
                //    "Name": "Jhon",
                //    "Start_Date": "Sun 01/06/08",
                //    "Finish_Date": "Sun 01/06/08",
                //    "Salary": "Trainee"
                //}];
                console.log(data);
                var model = new HomeModel();
                //ko.applyBindings(model);
                ko.applyBindings({
                    rows: data.GetEmployeesJSONResult
                });
            }
        });
    });


<table>
        <thead>
            <tr>
                <th>Employee ID</th>
                <th>Name</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: rows">
            <tr>
                <!--<td data-bind="text: Id"></td>
                <td data-bind="text: Name"></td>
                <td data-bind="text: Salary"></td>-->
                <td>
                    <pre data-bind="text: JSON.stringify(ko.toJS($data))"></pre>
                </td>
                <td>
                    <pre data-bind="text: JSON.stringify(ko.toJS($data), null, 2)"></pre>
                </td>
                <td>
                    <pre data-bind="text: JSON.stringify(ko.toJS($data), null, 2)"></pre>
                </td>
            </tr>
        </tbody>
    </table>