为什么我的xml请求未定义

时间:2016-05-22 02:30:18

标签: javascript php xml

我的代码现在不完整,但我测试了我的xml请求,我只是返回undefined。我试图访问服务器上的文件夹,该文件夹包含几个文件夹,每个文件夹以书籍命名,每个文件夹包含书籍信息(cover.jpg,一般信息txt,评论文本等)。现在我只是尝试使用以下格式在我的php中创建XML:

<books>
    <book>
        <title>whatever the title of the book is</title>
        <folder>whateverthefoldersnameis</folder>
    </book> 
</books>

但它似乎没有起作用,因为在第47行的javascript中,当我尝试获取书籍的文本内容时,我可以将其插入到我的HTML中,它只是说它未定义。在这里有点失落。

(function() {
var xml = null;
"use strict";

// sets up onclick handlers
window.onload = function() {
    getRequest("books");
    var home = document.getElementById("back");
    home.onclick = getRequest("books");
};

// sends an ajax request to the passed in address.
// calls the passed in function when the request returns.
function ajax($adress, $function) {
    var request = new XMLHttpRequest();
    request.onload = $function;
    request.open("GET", $adress, true);
    request.send(); 
}

// makes a request for all books.
function getRequest(value) {
    ajax("bestread.php?mode=" + value, displayXml("allbooks")); 
}

function displayJson() {
    $json = JSON.parse(this.responseText); 
    for($cat in $json.books) {
        var book = document.createElement("div");
        book.innerHTML = $cat; 
        book.onclick = choose;
        document.getElementById("allbooks").appendChild(book);
    } 
}

function choose() {
    var book = this.innerHTML;
    document.getElementById("allbooks").innerHTML = "";
    document.getElementById("singlebook").innerHTML = book;
    getRequest("info", getInfo);
    getRequest("reviews", displayXml("reviews"));

}

function displayXml(id) {
    xml = this.responseXML;
    document.getElementById(id).innerHTML = xml.querySelector("books").textContent;
}
})();


<?php

$mode = $_GET["mode"];
$book = $_GET["title"];
$url = "../../students/jck789/hw6/books/";


if($mode == "books") {
    bookXml($url);
} else if ($mode == "info") {
    outputJson($url, $book, "info");
} else if ($mode == "description") {
    outputJson($url, $book, "description");
} else if ($mode == "reviews") {
    reviewXml($url, $book, "reviews");
}

# outputs the list of available categories in JSON
function outputJson($url, $value) {
    $files = glob($url . "*");
    $json = array($value => array());
    foreach($files as $file) {
        $count = count(glob($file."/*"));
        $json[$value][basename($file)] = $count;
    }

    header("Content-type: application/json");
    print(json_encode($json));
}

function bookXml($url) {
    $files = glob($url . "*");
    $index = 0; //currently just trying to grab the first book
    list($name, $fold) = file($files[$index]);

    $dom = new DOMDocument();
    $books = $dom->createElement("books");
    $dom->appendChild($books);
    $book = $dom->createElement("book");
    $books->appendChild($book);
    $title = $dom->createElement("title");
    $title->appendChild($dom->createTextNode($name));
    $folder = $dom->createElement("folder");
    $folder->appendChild($dom->createTextNode($fold));
    $book->appendChild($title);
    $book->appendChild($folder);

    header("Content-type: text/xml");
    print($dom->saveXML());
}

function reviewXml($url, $book) {
    $files = glob($url . "$book/*")
}

?>

0 个答案:

没有答案