$ .getJSON说明属性未定义

时间:2014-09-26 16:31:43

标签: jquery ajax json

我使用此代码与API进行通信。我试图输出网址

$.getJSON("TilesHandlers/GetLastAccessedCourses.ashx?history=yes", function (data) {

        $.each(data.last_courses.Items, function (key, val) {
             console.log(val.url);

        });

    });

当我在chrome中输入代码时,我得到数字18,然后在响应中未定义。

这是json输出:

{
    "last_courses": {
        "Tile_title": "",
        "Items": {
            "Type": "Course",
            "IsOnline": "True",
            "CourseID": "55b40830-1c34-43ca-ba9d-342a69ba0a79",
            "Title": "Course C1",
            "Code": "c1",
            "Description": "description",
            "ProgramID": "00000000-0000-0000-0000-000000000000",
            "CourseProgram": "Provide-commerce",
            "DateEnrolled": "07/08/2014",
            "CourseStartDate": "07/08/2014",
            "LastModule": "",
            "LastModuleName": "",
            "LastDateAttempted": "",
            "AllowModal": "",
            "StatusDetail": "overdue",
            "url": "launchEnterClassrooom('55b40830-1c34-43ca-ba9d-342a69ba0a79', '00000000-0000-0000-0000-000000000000', '', '1', '600', '800', '', 'Course C1')",
            "Status": "Active",
            "PercentCompleted": "0"
        },
        "New": "0",
        "Started": "0",
        "Completed": "0",
        "Overdue": "1",
        "Due": "0",
        "Expired": "0"
    }
}

2 个答案:

答案 0 :(得分:1)

好像你正在尝试获取网址,只需执行console.log(data.last_courses.Items.url)

你的代码抛出的错误是item是一个objct而不是一个数组

答案 1 :(得分:0)

Itemsobject而不是array

您无需迭代它。

所以你可以直接获取url属性值。

你必须这样做:

console.log(data.last_courses.Items.url)

样本:

http://jsfiddle.net/npv34udg/