我试图用JS(Node和ajax(和jQuery库))替换我所有的PHP,但在将以下PHP脚本转换为ajax时遇到困难发动机。将PHP脚本转换为读取和打印XML数据的JavaScript脚本
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("Administration/data/people.xml");
$xx=$xmlDoc->getElementsByTagName('person');
$hintt="";
for($ii=0; $ii<($xx->length); $ii++)
{
$yy=$xx->item($ii)->getElementsByTagName('id');
$zz=$xx->item($ii)->getElementsByTagName('fullName');
if ($yy->item(0)->nodeType==1)
{
echo "<button type='button' class='mybutton' name='users'>" .
$zz->item(0)->childNodes->item(0)->nodeValue . "</button>";
}
}
?>
这里是我的ajax尝试:
<div id="loadMe">
<h1>Reading..</h1>
</div>
<script>
$.ajax({
type: "GET",
url: "Administration/data/people.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('person').each(function(){
var fullName = $(this).attr('fullName');
$("<button type=button class=mybutton value='+fullName+'></button>").html("<h3>'+fullName+'</h3>").appendTo('#loadMe');
});
}
});
</script>
对我来说,它看起来非常相似,但JS是行不通的。任何人看到不一致或可以告诉我为什么我的XML元素不附加到指定的div标签?提前感谢球员和球员!编辑(1/24/14 1:24 AM): 我认为提供我的XML会有帮助,也许我引用的数据是错误的?
<people>
<person>
<id>10</id>
<fullName>Philadelphia Collins</fullName>
<firstName>Philadelphia</firstName>
<lastName>Collins</lastName>
<age>62</age>
<hometown>Sunnyvale</hometown>
<job>Restraunt Owner</job>
</person>
<people>
你应该做'.done()'和'.fail()'而不是'成功:' –
'$('
我用过,但这不是问题的原因@Blazemonger你是否看到任何逻辑错误? – Jim22150