I want to load a xml file using Jquery . The xml is generated using a php script (Currently just using echo) My problem is that the file will just not produce and results . My Jquery is listed below
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "phpxml.php",
dataType: "xml",
success: displayXml
});
function displayXml(data){
$(data).find("sites").each(function() {
var heading = $(this).find("name").text();
var output = "<li>";
output += "<h2>" + heading + "</h2>";
output += "</li>";
$("#place-holder").append(output);
});
}
}); // doc ready
</head>
<body>
<h2> Ajax / Jquery Test </h2>
<ul id='place-holder'>
</ul>
</html>
My php file is below which just echos xml. Can anyone offer any suggestions please ?
<?php
echo '<?xml version="1.0" encoding="iso-8859-1" ?>';
echo"
<sites>
<name>
J Smith
</name>
<name>
A Coxhead
</name>
<name>
D Wilson
开发者_开发技巧 </name>
</sites>
" ;
?>
Well, it seems that you haven't set content -type in phpxml.php file.
try,
<?php
header('content-type:text/xml');
echo '<?xml version="1.0" encoding="iso-8859-1" ?>';
echo"
<sites>
<name>
J Smith
</name>
<name>
A Coxhead
</name>
<name>
D Wilson
</name>
</sites>
" ;
?>
Make sure that there should be no spaces before php tag other wise headers will not be sent.
what does Firebug or equivalent tell you about the response? First things first, is your ajax request getting a response?
also, put a breakpoint (or alert) in your displayXml function to make sure it is firing...
try to add error callback to $.ajax
.
If this is exacly what your code look like, then you have spaces before <?
in php file.
xml file must have <?xml
as first characters
you also don't have <script type="text/javascript"> ... </script>
tags
精彩评论