これは私の JavaScript コードです。サーバーから受け取ったデータを使用して HTML で動的リストを作成しようとしています。データのタイプは「json」です
私のJavascriptスニペット
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);
            }
        //}*/
私のhtmlコードは
<!DOCTYPE html>
<head>
   <script ...> 
 <head>                  
<body onLoad="addBooks()">
    <div id="addBooks" class="row-fluid">
        <div id="gBookList">
        </div>
    </div>
</body>
</html>
次のエラーが表示され続け、リストにデータを入力できません。クロムを使用しています
1) キャッチされていない TypeError: null のメソッド 'appendChild' を呼び出せません
2) キャッチされていない TypeError: 未定義のプロパティ 'length' を読み取れません
alert box を使用してデバッグすると、.length コマンドが正しい整数 (json オブジェクトの量) を返すため、なぜこれが発生するのかわかりません。
それを呼び出す関数
$.ajax({
    type: 'GET',
    url: ........,
    dataType: "json",
    complete: function (xhr, statusText) {
        alert(xhr.status);
    },
    success: function (data, textStatus, jqXHR) {
        alert(JSON.stringify(data));
        window.location.replace("Page2_updated.html");
        addBooks(data); // Passing JSON to be replaced on page
    },
    function (data, textStatus, jqXHR) {
        alert(data);
        alert('error');
    },
});
編集
このフォーラムでアドバイスを受けた後、HTML ファイルを次の構造に変更しました。
<html>
<head>
</head>
<body>
<div id="1" "display:block">
</div>
<div id="2" "display:none">  // no onLoad() anymore!!
</div>
</body>
</html>
この部分を呼び出し関数で編集しました
 $.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');
        },
    });
しかし、私はまだ次のエラーが発生します Uncaught TypeError: Cannot call method 'appendChild' of null Uncaught TypeError: Cannot read property 'length' of undefined