Javascript 为什么我的xml请求返回时未定义
我的代码现在是不完整的,但我测试了我的xml请求,我只是返回未定义的。我试图访问服务器上的一个文件夹,其中包含几个文件夹,每个文件夹都以一本书命名,每个文件夹都包含该书的信息(cover.jpg、general info txt、reviews txt等)。现在,我正尝试在php中创建一个XML,格式如下:Javascript 为什么我的xml请求返回时未定义,javascript,php,xml,Javascript,Php,Xml,我的代码现在是不完整的,但我测试了我的xml请求,我只是返回未定义的。我试图访问服务器上的一个文件夹,其中包含几个文件夹,每个文件夹都以一本书命名,每个文件夹都包含该书的信息(cover.jpg、general info txt、reviews txt等)。现在,我正尝试在php中创建一个XML,格式如下: <books> <book> <title>whatever the title of the book is</title
<books>
<book>
<title>whatever the title of the book is</title>
<folder>whateverthefoldersnameis</folder>
</book>
</books>
不管这本书的书名是什么
这是什么
但它似乎不起作用,因为在我的javascript第47行中,当我尝试获取书籍的文本内容以便将其插入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/*")
}
?>
(函数(){
var xml=null;
“严格使用”;
//设置onclick处理程序
window.onload=函数(){
获取请求(“书籍”);
var home=document.getElementById(“返回”);
home.onclick=getRequest(“books”);
};
//向传入的地址发送ajax请求。
//当请求返回时调用传入函数。
函数ajax($address,$function){
var request=new XMLHttpRequest();
request.onload=$function;
打开(“GET”,$address,true);
request.send();
}
//请求所有书籍。
函数getRequest(值){
ajax(“bestread.php?mode=“+value,displayXml(“allbooks”));
}
函数displayJson(){
$json=json.parse(this.responseText);
for($json.books中的cat){
var book=document.createElement(“div”);
book.innerHTML=$cat;
book.onclick=选择;
document.getElementById(“所有书籍”).appendChild(书籍);
}
}
函数选择(){
var book=this.innerHTML;
document.getElementById(“allbooks”).innerHTML=“”;
document.getElementById(“singlebook”).innerHTML=book;
getRequest(“info”,getInfo);
getRequest(“评论”,displayXml(“评论”);
}
函数displayXml(id){
xml=this.responseXML;
document.getElementById(id).innerHTML=xml.querySelector(“books”).textContent;
}
})();
displayXml(“allbooks”)
将立即调用该函数。。。使用/替换为function(){displayXml(“allbooks”)}
@Rayon是的,我也尝试过,但仍然没有得到任何结果。属性返回一个包含请求响应的文档。而xml=this.responseXML上的this
代码>由窗口引用
而非XHR请求。怎么样?等等,我对你的意思有点困惑,但谢谢你在修复了一些东西后,我刚刚得到了“null”现在php有问题吗?displayXml(“allbooks”)
将立即调用该函数。。。使用/替换为function(){displayXml(“allbooks”)}
@Rayon是的,我也尝试过,但仍然没有得到任何结果。属性返回一个包含请求响应的文档。而xml=this.responseXML上的this
代码>由窗口引用
而非XHR请求。怎么样?等等,我对你的意思有点困惑,但是谢谢你,在修复了一些东西之后,我刚刚得到了“null”,现在php有问题吗?