html页面之间传递的JSON数据

JSON data passing between html pages

本文关键字:JSON 数据 之间 html      更新时间:2024-02-12

可能重复:
Javascript DOM错误

我有以下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. Uncaught TypeError:无法调用null的方法"appendChild"
  2. 未捕获的TypeError:无法读取未定义的属性"length"

我昨天已经发布了这个问题,但我只是想对错误有一个新的看法。

这里可能有一些问题:-

  • 你在做跨域的工作吗?如果您考虑使用JSONP,这将解决您可能看到的由于安全策略而"扣留"数据的一些问题。

  • 你也在访问一个变量,并假设它有一个值

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

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

必须取消注释

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

在中

link.appendChild(pic);

这个元素还没有贴花(在你的情况下只是评论)!