Json数据在html页面之间传递

时间:2012-07-24 18:41:17

标签: javascript jquery html google-chrome javascript-events

  

可能重复:
  Javascript DOM errors

我有以下的html代码

<html>
<head>
</head>
<body>
<div id="1" "display:block">
</div>
<div id="2" "display:none">  // no onLoad() anymore!!
</div>
</body>
</html>

我的Javascript代码如下

$.ajax({
        type: 'GET',
        url: ........,
        dataType: "json",
        complete: function (xhr, statusText) {
            alert(xhr.status);
        },
        success: function (data, textStatus, jqXHR) {
            alert(JSON.stringify(data));
            if(document.getElementById(1).style.display == "block"){ 
                document.getElementById(1).style.display = "none"; 
                document.getElementById(2).style.display = "block"; }
            addBooks(data); // Passing JSON to be replaced on page
        },

        function (data, textStatus, jqXHR) {
            alert(data);
            alert('error');
        },
    });

function addBooks(data) { // USing DOM to populate the tables 


    //var  newdata=document.getElementById('addBooks');
    //newdata.setattribute()

    //get the unordered list

    var newdata = document.getElementById('addBooks');
    var parent = document.getElementById('gBookList');
    //removeChildrenFromNode(parent);

    //create list divider
    var listdiv = document.createElement('li');
    listdiv.setAttribute('id', 'gBookListDiv');
    listdiv.innerHTML = ("Books Found:");
    parent.appendChild(listdiv);
    // (this is where the first error happens)

    //create dynamic list

    for (i = 0; i < data.length; i++) {
        // (this is where the second error happens)



        //create each list item 
        var listItem = document.createElement('li');
        listItem.setAttribute('id', 'gBookListItem');
        parent.appendChild(listItem);
        //var link = document.createElement('a');
        //link.setAttribute('onclick','displayBook(data[i])');
        //link.setAttribute('href','#FindBook)');
        //listItem.appendChild(link);
        var pic = document.createElement('img');
        pic.setAttribute('src', data[i].pictureURL);
        pic.setAttribute('width', '80px');
        pic.setAttribute('height', '100px');
        pic.setAttribute('style', 'padding-left: 10px');
        link.appendChild(pic);
        var brk = document.createElement('br')
        link.appendChild(brk);
        var title = document.createElement('p');
        title.innerHTML = data[i].title;
        title.setAttribute = ('style', 'float:right');
        link.appendChild(title);
        var author = document.createElement('p');
        author.innerHTML = data[i].author;
        link.appendChild(author);
    }
    var list = document.getElementById('gBookList');
    // $(list).listview("refresh");
}

/*function removeChildrenFromNode(node){
            while (node.hasChildNodes()){
                node.removeChild(node.firstChild);
            }
        //}*/

我一直收到以下错误,导致我无法填充列表,我正在使用chrome

1)未捕获的TypeError:无法调用方法&#39; appendChild&#39;为null 2)未捕获的TypeError:无法读取属性&#39;长度&#39;未定义的

昨天我已经发布了这个问题,但我只想对错误可能是什么进行全新的了解

2 个答案:

答案 0 :(得分:0)

这里可能会有一些问题: -

  • 你在做这个跨领域吗?如果你考虑使用JSONP,它将解决你可能会看到的一些问题,因为安全策略导致数据被“隐瞒”。

  • 您还访问变量并假设它具有值

    var parent = document.getElementById('gBookList');

    在使用父标记之前,应确保它不是null(或空引用),并且元素(gBookList)在文档中确实存在。

答案 1 :(得分:0)

必须取消注释

// var link = document.createElement('a');

link.appendChild(pic);

此元素尚未声明(在您的情况下只是注释)!