问题描述
所以我使用PHP + MySQL来提供的XML数据库的内容到JavaScript。
So I'm using PHP+MySQL to deliver database contents in XML to a JavaScript.
$xml = "<?xml version='1.0' encoding='utf-8'?><confessions><pending>";
$pending = $m->MySqlHandler->Query("SELECT id, gender, age, confession, date_posted FROM confessions WHERE publish = 0");
foreach ($pending->Rows as $pr)
{
list($id, $gender, $age, $confession, $dateposted) = array(
$pr->Columns["id"]->Value,
$pr->Columns["gender"]->Value,
$pr->Columns["age"]->Value,
$pr->Columns["confession"]->Value,
$pr->Columns["date_posted"]->Value
);
$xml .= "<confession id='$id' gender='$gender' age='$age' dateposted='$dateposted'>$confession</confession>";
}
$ XML =&LT; /未决>;$ XML =&LT;积极>
$xml .= "</pending>";$xml .= "<active>";
$积极= $ M-> MySqlHandler->查询( SELECT DISTINCT招供。*, (SELECT COUNT(*)FROM评论WHERE confession_id = confessions.id)作为注释, (SELECT COUNT(*)FROM语句的WHERE confession_id = confessions.id)AS语句 从告白WHERE confessions.publish = 1);
$active = $m->MySqlHandler->Query( "SELECT DISTINCT confessions.*, (SELECT COUNT(*) FROM comments WHERE confession_id = confessions.id) AS comments, (SELECT COUNT(*) FROM sentences WHERE confession_id = confessions.id) AS sentences FROM confessions WHERE confessions.publish = 1");
的foreach($主动 - >行为$ AR){ 列表($ ID,$性别,$年龄,$表白,$ dateposted,$ absolutions)=阵列( $ AR->列[身份证] - >值, $ AR->列[性别] - >值, $ AR->列[年龄] - >值, $ AR->列[告白] - >值, $ AR->列[dateposted] - >值, $ AR->列[absolutions] - >值 ); $ sql_template =SELECT COUNT(*)FROM语句的WHERE confession_id = $ ID和度='%s'的; $ sentence_data =阵列( 吨=> mysql_result(的mysql_query(的sprintf($ sql_template,吨)),0,0), 的C=> mysql_result(的mysql_query(的sprintf($ sql_template,C)),0,0), P=> mysql_result(的mysql_query(的sprintf($ sql_template,P)),0,0), 升=> mysql_result(的mysql_query(的sprintf($ sql_template,升)),0,0) ); $ XML =&LT;表白absolutions ='$ absolutions'T ='{$ sentence_data ['T']}' 。 C ='{$ sentence_data ['C']}'P ='{$ sentence_data ['P']}' 。 L ='{$ sentence_data ['L']}'ID ='$ ID'性别='$性别' 。 年龄='$时代'> $忏悔和LT; /表白>;}
foreach ($active->Rows as $ar){ list($id, $gender, $age, $confession, $dateposted, $absolutions) = array( $ar->Columns["id"]->Value, $ar->Columns["gender"]->Value, $ar->Columns["age"]->Value, $ar->Columns["confession"]->Value, $ar->Columns["dateposted"]->Value, $ar->Columns["absolutions"]->Value ); $sql_template = "SELECT COUNT(*) FROM sentences WHERE confession_id = $id AND degree = '%s'"; $sentence_data = array( "t" => mysql_result(mysql_query(sprintf($sql_template, "t")), 0, 0), "c" => mysql_result(mysql_query(sprintf($sql_template, "c")), 0, 0), "p" => mysql_result(mysql_query(sprintf($sql_template, "p")), 0, 0), "l" => mysql_result(mysql_query(sprintf($sql_template, "l")), 0, 0) ); $xml .= "<confession absolutions='$absolutions' t='{$sentence_data['t']}' " . "c='{$sentence_data['c']}' p='{$sentence_data['p']}' " . "l='{$sentence_data['l']}' id='$id' gender='$gender' " . "age='$age'>$confession</confession>";}
$ XML =。标题(内容类型:应用程序/ XML);回声$的xml;
$xml .= "";header("Content-Type: application/xml");echo $xml;
所以,从那里,你得到的结果,如...
So, from there, you get a result such as...
<?xml version='1.0' encoding='utf-8'?>
<confessions>
<pending>
<confession id="1" gender="m" age="20" dateposted="2010-02-06 05:22:57">
Confesando.
</confession>
</pending>
<active>
<confession absolutions="0" t="0" c="0" p="0" l="0" id="2" gender="m" age="18">
Confesion.
</confession>
</active>
</confessions>
我通过加载JavaScript的XML:
I load the XML with JavaScript by using:
function sendData(params, to, oncomplete)
{
if (typeof oncomplete == 'undefined')
{
oncomplete = function() {};
}
if (typeof window.ActiveXObject != 'undefined' ) {
http = new ActiveXObject("Microsoft.XMLHTTP");
http.onreadystatechange = oncomplete;
} else {
http = new XMLHttpRequest();
http.onload = oncomplete;
}
http.open("POST", to, false);
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-Length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
}
...这就是所谓这样的:
...which is called like this:
// Load approval-pending data //
sendData("", "./leer/confesiones/" + sId, function()
{
var xml = http.responseXML;
var pending = xml.getElementsByTagName("pending").getElementsByTagName("confession");
(...)
我会停下来就在这里。因为当我尝试分析我得到以下错误在萤火虫的XML:
I'll stop right here. Because when I attempt to parse the XML I get the following error at Firebug:
XML Parsing Error: no element found Location: moz-nullprincipal:{7e9eab45-2f73-476d-9bdb-2370d1534f29} Line Number 1, Column 1:
^
我尝试加载 ./阅读/ confesiones /
通过输入它作为一个网址到浏览器中,它就像一个魅力。完全有效的XML。用Firebug的网络进行检查XHR是这么说的也是,有效的XML。控制台视图是一个给我的错误,好像如果它是一个JavaScript错误。但 http.responseText
包含XML,文本,以及 XML
的类型是 [对象为XMLDocument]
。
I tried loading ./leer/confesiones/
by inputting it as an URL into the browser and it works like a charm. Fully valid XML. Using Firebug to inspect XHR under "Net" says so too, valid XML. The Console view is the one that gives me the error, like if it is a JavaScript error. But http.responseText
contains the XML, in text, and xml
is of type [object XMLDocument]
.
所以......我缺少什么?
So... what am I missing?
解决:修改PHP来输出JSON和JavaScript才能正确地解析它
SOLVED: modified PHP to output JSON and JavaScript to parse it properly.
推荐答案
请你帮个忙,并使用JS库,包装所有的AJAX魔术给你。有很多的跨浏览器问题和陷阱,这可能只是其中的一件事情。
Do yourself a favor, and use a JS library that wraps all the ajax magic for you. There's a lot of cross-browser issues and gotchas, and this may just be one of those things.
我建议jQuery的,这是最简单和相当强大。因此,添加到您的HTML的顶部,头部标签中:
I'd recommend jQuery, it's the easiest and quite powerful.So add this to the top of your html, inside the head tag:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
然后在你的JS做这样的事情:
And then in your JS do something like this:
$.get('/leer/confesiones/', function(data) {
console.log(data);
});
这应该让你开始。 看看这里有关jQuery的更多信息和$不用彷徨功能。BTW-我看您使用的是POST,但是对于检索数据(不更新或删除)该公约是GET。
That should get you started. Look here for more info about jQuery and the $.get function.BTW- I see you're using a POST, but for retrieval of data (no updating or deleting) the convention is GET.
此外,考虑改变你的PHP,这样它会返回JSON格式的数据,而不是XML。因此,而不是这样做,小舞,你必须做的XML标记,只是得到一个数组准备好所有的数据,而做到这一点:
Additionally, consider changing your PHP so that it returns JSON formatted data instead of XML.So instead of doing that little dance you have to do with the xml markup, just get an array ready with all the data, and do this:
echo json_encode($array); // voila
这篇关于XmlHTT prequest:&QUOT; XML解析错误:没有元素发现&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!