views:

123

answers:

2
+1  Q: 

jQuery and AJAX

Can anyone help with a jQuery snippet that would use Ajax to pull an XML file in on page load?

Have really clunky way of doing it without jQuery here:

<script type="text/javascript">
function  loadXMLDoc()
{
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
     xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      xmlDoc = xmlhttp.responseXML;
      var txt = "";
      x = mlDoc.getElementsByTagName("title");
      for (i=0;i<x.length;i++)
      {
        txt = txt + x[i].childNodes[0].nodeValue + "<br />";
      }
    document.getElementById("checkedIn").innerHTML=txt;
    }
  }
  xmlhttp.open("GET", "data.xml", true);
  xmlhttp.send();
}
</script>

Ideally rather the having a click generate the list it would do so on page load, showing the fields from the XML (title, author, and whether it is checked in or not)

Would hug you for a solution

+1  A: 

why dont you use jquery simple ajax request?

$(document).ready(function() {
    $.ajax({
        url: "file.xml",
        dataType: "xml",
        success: function() {
            // on success here
        }
    });
});
GerManson
I would also include the error function just in case.
James Westgate
+5  A: 
$( function() {
 $.ajax( { 
     url: 'ajax.xml',
     type: 'GET',
     dataType: 'xml',
     success: function( response ) {
        var books = $( response ).find( 'book' );
        var list = $( '#booklist' );
        $( books ).each( function() {
            var checkedOut = ( $( this ).attr( 'checked-out' ) == '1' );
            var title = $( this ).find( 'title' );
            var li = $( '<li></li>');
            if( checkedOut ) {
               li.addClass( 'selected' );
            }
            li.html( title );
            list.append( li );
        });
     }
 });
});
Jacob Relkin
I would also include the error function just in case.
James Westgate
Thanks, stumbled upon this shortly after asking via jQuery website...makes perfect sense. But I'm just not familiar enough with JS to actually write my success function. How might I display all the resulting <titles> from the XML file as a li in a ul named #booklist, then check if the <book> from the XML has a checked-in="0" or 1 and give them a different class on their li?
Fuego DeBassi
@Banderdash, What does your XML look like?
Jacob Relkin
@Banderdash, what is it that you want to display?
Jacob Relkin
I'd just like to show the title's from the XML in a list, and then add a class to a specific list-item of that "book" has checked-out="1"
Fuego DeBassi
@Banderdash, got it. Updated the code.
Jacob Relkin
I feel like we are close, http://clients.pixelbleed.net/ajax-test/ but not working?
Fuego DeBassi
Updated it again.
Jacob Relkin
@Banderdash, I'm waiting for you... :)
Jacob Relkin
Sorry! Had a quick bite to eat. Really appreciate you working with me on this. I've updated your update but it's still not outputting, any ideas? Maybe try test at http://jsbin.com ?
Fuego DeBassi
Added it here: http://jsbin.com/awivi3 where you can edit
Fuego DeBassi
Um, that will not work due to XSS.
Jacob Relkin
I'm going to try to get the XML file and do it locally.
Jacob Relkin
Who's the man? You da man.
Fuego DeBassi
I got it. Updated my answer.
Jacob Relkin
Brother, thank you! Totally understand it and it works great.
Fuego DeBassi
Awesome. Makes me happy that you're happy.
Jacob Relkin