在DDBB内部,a有以下数据:
SELECT `addedat`, `catname`, `catkey` FROM `categorias`;
"2014-06-23" "Complementos" "complementos"
"2014-06-23" "Hombre" "hombre"
"2014-06-23" "Mujer" "mujer"
"2014-06-23" "Niños y bebes" "niños_y_bebes"
获得以下功能脚本:
public function listAllCategories(){
$ret = null;
$result = self::$ddbb->executeQuery(self::$dao->getQueryGetAllCategories());
if ($result && (mysql_num_rows($result) !== 0)){
$categories = array();
while($row = mysql_fetch_row($result)){
$aux = new Categoria();
$aux->setCatDate($row[0]);
$aux->setCatName($row[1]);
$aux->setCatKey($row[2]);
array_push($categories, $aux);
}//while
mysql_free_result($result);
$ret1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$ret1 .= "\n<categories>";
foreach($categories as $category){
$ret1 .= "\n\t<category>";
$ret1 .= "\n\t\t<addedat>".$category->getCatDate()."</addedat>";
$ret1 .= "\n\t\t<name>".$category->getCatName()."</name>";
$ret1 .= "\n\t\t<key>".$category->getCatKey()."</key>";
$ret1 .= "\n\t</category>";
}//foreach
$ret1 .= "\n</categories>";
$ret = trim($ret1);
}else{
$ret = new Error(self::$errorFilePath, "ERROR: no se pudo listar las categorias. MySQL = ".self::$ddbb->getError());
}
return $ret;
}
在此功能之后,超级'controller.php'执行以下操作:
header("Content-Type", "text/xml");
header_response_code(200);
echo $ret;
但是该脚本返回以下XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<categories>
<category>
<addedat>2014-06-23</addedat>
<name>Niños y bebes</name>
<key>niños_y_bebes</key>
</category>
<category>
<addedat>2014-06-23</addedat>
<name>Niños y bebes</name>
<key>niños_y_bebes</key>
</category>
<category>
<addedat>2014-06-23</addedat>
<name>Niños y bebes</name>
<key>niños_y_bebes</key>
</category>
<category>
<addedat>2014-06-23</addedat>
<name>Niños y bebes</name>
<key>niños_y_bebes</key>
</category>
</categories>
jQuery声称无效的XML
答案 0 :(得分:0)
你应该使用一个可以像SimpleXML那样正确地将字符串编码为XML而不是进行字符串连接的库:
$ret = new SimpleXMLElement('<categories/>');
foreach ($categories as $category) {
$category = $ret->addChild('category');
$category->addedat = $category->getCatDate();
$category->name = $category->getCatName();
$category->key = $category->getCatKey();
}
$ret->asXML('php://output');
这个工作的唯一前提条件是$category
的getter(就像$category->getCatDate()
这样的方法)正在返回UTF-8编码的字符串。
如果他们不这样做你会看到错误 - 但你会很早就看到它们。另见:
并确保您已启用错误日志记录,以便在进行AJAX交互时跟踪错误。
答案 1 :(得分:0)
我认为问题出现在以下代码中:
$aux->setCatDate($row[0]);
$aux->setCatName($row[1]);
$aux->setCatKey($row[2]);
尝试使用列名从DB获取$ row数据,如:
$aux->setCatDate($row['addedat']);
$aux->setCatName($row['catname']);
$aux->setCatKey($row['catkey']);
然后看结果。