Javascript 为什么这个Ajax响应文本是空的?

Javascript 为什么这个Ajax响应文本是空的?,javascript,php,ajax,xml,Javascript,Php,Ajax,Xml,单击按钮时,将创建新的Ajax请求,并从books.php调用数据。 因此,我使用getElementByName收集所有named=“category”单选按钮 这是我的html代码 $("b_xml").onclick=function(){ new Ajax.Request("books.php", { method:"GET", parameters: {category:getCheckedRadio(document.g

单击按钮时,将创建新的Ajax请求,并从books.php调用数据。 因此,我使用getElementByName收集所有named=“category”单选按钮 这是我的html代码

$("b_xml").onclick=function(){
        new Ajax.Request("books.php", {
            method:"GET",
            parameters: {category:getCheckedRadio(document.getElementsByName("category"))},
            onSuccess: showBooks_JSON,
            onFailure: ajaxFailed
        })
    }
儿童
计算机
烹饪
财务
和books.php

<label><input type="radio" name="category" value="children" checked="checked"/> Children</label>
<label><input type="radio" name="category" value="computers" /> Computers</label>
<label><input type="radio" name="category" value="cooking" /> Cooking</label>
<label><input type="radio" name="category" value="finance" /> Finance</label>

我查过books.txt不是空的 当我点击按钮时,警报是工作的,但它返回空框。
问题是什么

从books.php返回JSON

<?php
$BOOKS_FILE = "books.txt";

function filter_chars($str) {
    return preg_replace("/[^A-Za-z0-9_]*/", "", $str);
}

if (!isset($_SERVER["REQUEST_METHOD"]) || $_SERVER["REQUEST_METHOD"] != "GET") {
    header("HTTP/1.1 400 Invalid Request");
    die("ERROR 400: Invalid request - This service accepts only GET requests.");
}

$category = "";
$delay = 0;

if (isset($_REQUEST["category"])) {
    $category = filter_chars($_REQUEST["category"]);
}
if (isset($_REQUEST["delay"])) {
    $delay = max(0, min(60, (int) filter_chars($_REQUEST["delay"])));
}

if ($delay > 0) {
    sleep($delay);
}

if (!file_exists($BOOKS_FILE)) {
    header("HTTP/1.1 500 Server Error");
    die("ERROR 500: Server error - Unable to read input file: $BOOKS_FILE");
}

header("Content-type: application/xml");

print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print "<books>\n";


$lines = file($BOOKS_FILE);
for ($i = 0; $i < count($lines); $i++) {
    list($title, $author, $book_category, $year, $price) = explode("|", trim($lines[$i]));
    if ($book_category == $category) {
        print "\t<book category=\"$category\">\n";
        print "\t\t<title>$title</title>\n";
        print "\t\t<author>$author</author>\n";
        print "\t\t<year>$year</year>\n";
        print "\t\t<price>$price</price>\n";
        print "\t</book>\n";        
    }
}
print "</books>";
?>
0){
睡眠(延迟);
}
如果(!file_存在($BOOKS_file)){
标题(“HTTP/1.1500服务器错误”);
die(“错误500:服务器错误-无法读取输入文件:$BOOKS_file”);
}
标题(“内容类型:应用程序/xml”);
$BOOKS\u XML+=“\n”;
$BOOKS\u XML+=“\n”;
$lines=文件($BOOKS\u file);
对于($i=0;$i<计数($line);$i++){
列表($title,$author,$book_category,$year,$price)=分解(“|”),修剪($line[$i]);
如果($book_category==$category){
$BOOKS\u XML+=“\t\n”;
$BOOKS\u XML+=“\t\t$title\n”;
$BOOKS\u XML+=“\t\t$author\n”;
$BOOKS\u XML+=“\t\t$year\n”;
$BOOKS\u XML+=“\t\t$price\n”;
$BOOKS\u XML+=“\t\n”;
}
}
$BOOKS_XML+=“”;
$JSON=JSON_编码(simplexml_加载_字符串($BOOKS_XML));
echo$JSON;
?>

success函数名为
showBooks\u JSON
——但是您的PHP明确地用XML响应。。。即使没有看到showBooks\u JSON的功能,很明显,您的浏览器控制台中可能存在您没有提到的错误,但是showBooks\u XML和showBooks\u JSON是完全相同的功能。它们同样返回ajaxresponsetext。在将showBooks_JSON更改为showBooks_XML后,我尝试运行此函数,但它同样返回空值您没有显示函数-它可能做错了
函数showBooks_XML(ajax){alert(ajax.responseText);}函数showBooks_JSON(ajax){alert(ajax.responseText)}
这是我的函数。他们是sameI,我猜第一步是通过一些基本的故障排除来缩小问题范围,而你似乎没有这样做。看看ajax请求。PHP是否返回您期望的结果?如果是这样,这就是Javascript问题。如果不是,那就是PHP问题。对不起,我不工作。它同样返回空值。
<?
$BOOKS_FILE = "books.txt"; $BOOKS_XML = "";

function filter_chars($str) {
    return preg_replace("/[^A-Za-z0-9_]*/", "", $str);
}

if (!isset($_SERVER["REQUEST_METHOD"]) || $_SERVER["REQUEST_METHOD"] != "GET") {
    header("HTTP/1.1 400 Invalid Request");
    die("ERROR 400: Invalid request - This service accepts only GET requests.");
}

$category = "";
$delay = 0;

if (isset($_REQUEST["category"])) {
    $category = filter_chars($_REQUEST["category"]);
}
if (isset($_REQUEST["delay"])) {
    $delay = max(0, min(60, (int) filter_chars($_REQUEST["delay"])));
}

if ($delay > 0) {
    sleep($delay);
}

if (!file_exists($BOOKS_FILE)) {
    header("HTTP/1.1 500 Server Error");
    die("ERROR 500: Server error - Unable to read input file: $BOOKS_FILE");
}

header("Content-type: application/xml");

$BOOKS_XML += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$BOOKS_XML += "<books>\n";


$lines = file($BOOKS_FILE);
for ($i = 0; $i < count($lines); $i++) {
    list($title, $author, $book_category, $year, $price) = explode("|", trim($lines[$i]));
    if ($book_category == $category) {
        $BOOKS_XML += "\t<book category=\"$category\">\n";
        $BOOKS_XML += "\t\t<title>$title</title>\n";
        $BOOKS_XML += "\t\t<author>$author</author>\n";
        $BOOKS_XML += "\t\t<year>$year</year>\n";
        $BOOKS_XML += "\t\t<price>$price</price>\n";
        $BOOKS_XML += "\t</book>\n";        
    }
}
$BOOKS_XML += "</books>";

$JSON = json_encode(simplexml_load_string($BOOKS_XML));
echo $JSON;
?>