繁体   English   中英

为什么这个 Ajax ResponseText 是空的?

[英]Why this Ajax ResponseText is empty?

$("b_xml").onclick=function(){
        new Ajax.Request("books.php", {
            method:"GET",
            parameters: {category:getCheckedRadio(document.getElementsByName("category"))},
            onSuccess: showBooks_JSON,
            onFailure: ajaxFailed
        })
    }

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

<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>

和书籍.php

<?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>";
?>

我检查了 books.txt 不是空的,当我单击按钮时,警报有效但它返回空框。 什么是问题?

从你的 books.php 返回 JSON

<?
$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;
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM