Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 如何在敲除js中获得json响应?_Javascript_Json_Knockout.js_Getjson_Knockout Mapping Plugin - Fatal编程技术网

Javascript 如何在敲除js中获得json响应?

Javascript 如何在敲除js中获得json响应?,javascript,json,knockout.js,getjson,knockout-mapping-plugin,Javascript,Json,Knockout.js,Getjson,Knockout Mapping Plugin,我想得到一个从json到knockout js的数组。为此,我使用这段代码,并尝试在AdobeCQ中实现。但我无法得到回应,所以任何人都可以帮忙这是我的代码 在jsp中 <table id="timesheets" class="table table-striped table-hover table-condensed"> <thead> <tr> <th>First Name</th>

我想得到一个从json到knockout js的数组。为此,我使用这段代码,并尝试在AdobeCQ中实现。但我无法得到回应,所以任何人都可以帮忙这是我的代码

在jsp中

 <table id="timesheets" class="table table-striped table-hover table-condensed">   
<thead>
    <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Month</th>
        <th>Year</th>
    </tr>
</thead>
   <tbody data-bind="foreach: viewModel.timesheets">
    <tr>
        <td data-bind="text: firstname"></td>
        <td data-bind="text: lastname"></td>
        <td data-bind="text: month"></td>
        <td data-bind="text: year"></td>
    </tr>
</tbody>
 </table>

名字
姓
月
年
手稿

<script>
 $(function () {
    ko.applyBindings(viewModel);
    viewModel.loadTimesheets();
});
function timesheet(timesheet) {
    this.id = ko.observable(timesheet.id);
    this.firstname = ko.observable(timesheet.firstname);
    this.lastname = ko.observable(timesheet.lastname);
    this.month = ko.observable(timesheet.month);
    this.year = ko.observable(timesheet.year);
}
var viewModel = {
    timesheets: ko.observableArray([]),

loadTimesheets: function () {
    var self = this;
    $.getJSON("/content/personal/test0/jcr:content/content-page/horizontalline.json",function (timesheets) {
            self.timesheets.removeAll();
            $.each(timesheets, function (index, item) {
                self.timesheets.push(new timesheet(item));
            });
        }
    );
}
};

</script>

$(函数(){
应用绑定(视图模型);
viewModel.loadTimesheets();
});
功能时间表(时间表){
this.id=ko.可观察(timesheet.id);
this.firstname=ko.observable(timesheet.firstname);
this.lastname=ko.observable(timesheet.lastname);
本月=可观察的工时(时间表月);
this.year=ko.可观测(时间表.年);
}
var viewModel={
时间表:ko.observableArray([]),
加载时间表:函数(){
var self=这个;
$.getJSON(“/content/personal/test0/jcr:content/content-page/horizontalline.json”,函数(时间表){
self.timesheets.removeAll();
$。每个(时间表、功能(索引、项目){
self.timesheets.push(新时间表(项目));
});
}
);
}
};
在json文件中

<%@page session="false" %>
<%@ page import="org.apache.sling.jcr.api.SlingRepository" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.day.cq.commons.TidyJSONWriter,
    com.day.cq.tagging.Tag,
    java.util.Locale,
    com.day.cq.wcm.api.WCMMode,
    com.day.cq.tagging.TagManager,
    com.day.cq.tagging.TagCloud" %>

<%

//Local variables
final SlingRepository repos = sling.getService(SlingRepository.class);
final UserManagerFactory umFactory = sling.getService(UserManagerFactory.class);
String pagepath = properties.get("propagePath","");
Session session = null;
List<String> list = new ArrayList<String>();
String listString = "";

try
{

    // Ensure that the currently logged on user has admin privileges.
    session = repos.loginAdministrative(null);

    final TidyJSONWriter writer = new TidyJSONWriter(response.getWriter());

list.add("a");
list.add("b");
list.add("a");
list.add("b");
list.add("a");
list.add("b");

    //Begin writing JSON response
    writer.object();
    writer.key("tagarray").array();
    Iterator<String> it = list.iterator();

        while(it.hasNext()) {
        listString += it.next();
            if(it.hasNext()) {
            listString += ",";
            }
        }
    writer.value(listString);
    writer.endArray();
    writer.endObject();
    session.logout();
}
catch (Exception e)
{
    System.out.println("myajaxsample Exception Occured: " + e.getMessage());
}
finally
{
    session.logout(); 
    session = null;
}
%>


请帮我解决这个问题,因为我是淘汰赛新手

您需要这样的脚本

function timesheet(timesheet) {
    this.id = ko.observable(timesheet.id);
    this.firstname = ko.observable(timesheet.firstname);
    this.lastname = ko.observable(timesheet.lastname);
    this.month = ko.observable(timesheet.month);
    this.year = ko.observable(timesheet.year);
}
var viewModel = {
    timesheets: ko.observableArray([]),
    loadTimesheets: function () {
        var self = this;
        $.getJSON("/content/aib/personal/test0/jcr:content/content-page/horizontalline.json",function (timesheets) {
                self.timesheets.removeAll();
                self.timesheets(timesheets)
            }
        );
    }
};

 $(function () {
    ko.applyBindings(viewModel);
    viewModel.loadTimesheets();
});

嗨,Raheel,它不起作用我有以下未捕获的引用错误:无法处理绑定“text:function(){return firstname}”消息:firstname未定义你能发布json的响应吗?我不知道如何查看响应。但是,当我使用jquery调用json时,它工作正常
console.log(timesheets)
,这将在浏览器的控制台中显示结果。按F12并选择console选项卡,然后刷新页面,您将看到执行此操作的结果
console.log(json.stringify(timesheets))
确保您不使用IE