Javascript AjaxResponseText为空 儿童 计算机 烹饪 财务 图书目录(XML) 图书列表(JSON)
这是我的html代码的一部分Javascript AjaxResponseText为空 儿童 计算机 烹饪 财务 图书目录(XML) 图书列表(JSON),javascript,php,ajax,Javascript,Php,Ajax,这是我的html代码的一部分 <label><input type="radio" name="category" value="children" checked="checked"/> Children</label> <label><input type="radio" name="category" value="computers" /> Computers</label> <label><inp
<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>
<button id="b_xml">List Books (XML)</button>
<button id="b_json">List Books (JSON)</button>
window.onload=function(){
$(“b_xml”).onclick=function(){
新的Ajax.Request(“books.php”{
方法:“获取”,
参数:{category:getCheckedRadio(document.getElementsByName(“category”)},
onSuccess:showBooks\u XML,
onFailure:ajaxFailed
})
}
$(“b_json”).onclick=function(){
//构造一个原型Ajax.request对象
}
};
函数getCheckedRadio(单选按钮){
对于(变量i=0;i<单选按钮长度;i++){
如果(单选按钮[i]。选中){
返回单选按钮[i]。值;
}
}
返回未定义;
}
函数showBooks_XML(ajax){
警报(ajax.responseText);
}
函数showBooks_JSON(ajax){
警报(ajax.responseText);
}
函数ajaxFailed(ajax,异常){
var errorMessage=“发出Ajax请求时出错:\n\n”;
如果(例外){
errorMessage+=“异常:”+Exception.message;
}否则{
errorMessage+=“服务器状态:\n”+ajax.status+“”+ajax.statusText+
“\n\n服务器响应文本:\n”+ajax.responseText;
}
警报(错误消息);
}
这是我的javascript代码。
它也使用prototype.js
window.onload = function() {
$("b_xml").onclick=function(){
new Ajax.Request("books.php", {
method:"GET",
parameters: {category:getCheckedRadio(document.getElementsByName("category"))},
onSuccess: showBooks_XML,
onFailure: ajaxFailed
})
}
$("b_json").onclick=function(){
//construct a Prototype Ajax.request object
}
};
function getCheckedRadio(radio_button){
for (var i = 0; i < radio_button.length; i++) {
if(radio_button[i].checked){
return radio_button[i].value;
}
}
return undefined;
}
function showBooks_XML(ajax) {
alert(ajax.responseText);
}
function showBooks_JSON(ajax) {
alert(ajax.responseText);
}
function ajaxFailed(ajax, exception) {
var errorMessage = "Error making Ajax request:\n\n";
if (exception) {
errorMessage += "Exception: " + exception.message;
} else {
errorMessage += "Server status:\n" + ajax.status + " " + ajax.statusText +
"\n\nServer response text:\n" + ajax.responseText;
}
alert(errorMessage);
}
这是php代码
畸形经济学|史蒂文·莱维特|金融| 2006 | 18.00
《哈利·波特与魔法石》| J.K.罗琳|儿童| 1998 | 19.99
晚餐早餐|阿曼达营地|烹饪| 2009 | 22.00
网络编程一步一步| Marty Stepp和Jessica Miller | computers | 2009 | 999.99
构建Java程序| Stuart Reges和Marty Stepp | computers | 2007 | 90.00
Core Java,第8版| Cay Horstmann | computers | 2007 | 35.00
傻瓜个人理财|埃里克·泰森|金融| 2006 | 14.95
21世纪的21个汉堡|斯图尔特·雷格斯|烹饪| 2010 | 75.00
巧克力的四大食品类别|维多利亚基斯特|烹饪| 2005 | 30.00
走,狗,走|P.D.伊斯曼|儿童| 1961 | 8.99
这是books.txt简要解释一下这段代码 在javascript中,它要求books.php分类值只在html中选中单选按钮。
和books.php打印值,
成功时,成功时,
showBooks_XML函数被称为警报AjaxRequestText
但它只会像这样返回。
我猜,一旦发生警报事件,
这意味着流已经通过onSucceed,这意味着books.php很好地接收了ajax请求。
所以我认为这不是ajax的问题。
但我找不到问题出在哪里。
请给我一些提示。检查开发人员工具中的ajax请求响应,例如,对于google open developer工具,转到网络选项卡,然后让您的web应用程序执行ajax请求,在所有请求列表中找到books.php请求,单击它并检查响应,或者更好,安装fiddler-然后你可以检查你所有的新工作流量,但我先警告你,fiddler会上瘾,会显示你的计算机有多少网络流量。。
<?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>$ti
tle</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>";
?>