1

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>
            " ; 

            ?>
1
  • Did displayXml get called? Is there anything written to #place-holder? Commented Sep 19, 2010 at 13:58

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Content-Type is nessery if you don't put dataType in $.ajax
0

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...

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.