Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to replace all of my PHP with JS (Node and ajax (and jQuery library)) but am having trouble converting the following PHP script into an ajax engine.

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

        }
      }

    ?>

Here is my ajax attempt:

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

To me, it looks pretty similar, but the JS is not working. Anyone see an inconsistency or can tell me why my XML elements are not appending to the indicated div tag? Thanks a lot in advance guys and gals!

EDIT (1/24/14 1:24 AM): I thought providing my XML would be helpful, perhaps I am referencing the data wrong?

<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>
share|improve this question
    
you should do .done() and .fail() instead of just success: –  Ilan Biala Jan 23 at 21:51
2  
The line beginning $('<button type='button' needs to use double-quotes inside the string. You should have seen an error in your browser's console about this. –  Blazemonger Jan 23 at 21:51
    
I used both but this is not the cause of the problem. @Blazemonger do you see any logical errors? –  Jim22150 Jan 23 at 21:54
    
Also, your <script> should be moved to the bottom of the page or wrapped in a $(document).ready call. –  Blazemonger Jan 23 at 21:54
1  
This script is run after jquery has been loaded, correct? That could be the issue; what do your devtools/javascript console say? –  furydevoid Jan 23 at 22:09

1 Answer 1

I've set up a test page that you can try and play around with if at any time you need to do more exploring. So here comes the solution.

First, I tried doing an AJAX request, which always came back as failed because the XML was invalid. If I just tried to access the XML, it said it was not valid and could not be displayed. The issue was the closing <people> tag was missing a /. It should be </people>'. So that fixed the AJAX request failing. The next part was parsing the XML response. I did this by using the.children()method in jQuery and then getting the text of the element with.text()`. It ends up looking something like this:

var fullName = $(this).children('fullName').text();

So with all that and anything else I may have missed but is available at the test page, I hope you can fix your issue and get a successful AJAX request going. If I missed anything or I wasn't clear, let me know and I'll try to explain a little more.

By the way, doing console.log(); in Chrome DevTools and using breakpoints to access the data myself always helps me see what I'm working with, that way I know how to go about getting what data I want from a request.

Good luck!

share|improve this answer
    
did my answer work for you? –  Ilan Biala Jan 26 at 22:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.