Containing an XML nodevalue with jQuery
My Predicament:
I have some things on my server that I want to connect with an AJAX call:
an XML file
an HTML test index
a folder full of HTML copy
some jQuery script
The XML holds the relative addresses of the HTML copy. The jQuery is supposed to grab that nodevalue and some DIVs I have on the test index page. Ultimately, the index page is supposed to update some of its HTML without a full reload, like what my AJAX studying has said would happen.
Unfortunately, I have almost no idea what I'm doing.
This is what I'm working with:
$(document).ready(function(){
$('#header').click(function(){
$('p#test').html('At least <em>this</em> works');
$.ajax({
type: "GET",
url: "archives.xml",
datatype: "xml",
success: function(xml){
$('div#viewer').append($(this).find('title')).text();
}
});
});
});
My AJAX call doesn't do anything. I can tell I'm doing something wrong, but at this point I'm just happy I stamped out the DOM 4 errors and Unexpected Token Errors.
And here's the test XML I'm trying to call and navigate and take data from:
<archives>
<entry>
<date>
<year>2011</year>
<month>April</month>
<day>1</day>
</date>
<title>Trees!</title>
<deck>and something that definitely isn't a tree</deck>
<source>./copy/1april2011.html</source>
</entry>
</archives>
What I already know:
I know javascript can't access a local filesystem. These are all issues I'm running into online.
jQuery selectors supposedly don't play nice with the XML DOM? At least that's what I gathered from various blogs from the last five or six years out there.
I know about JSON, but I'm having a hard time translating from XML to JSON. The whole braces and brackets syntax is a bit confusing.
Questions:
What's wrong with my success function? It mimics tutorials I've read, but I've clearly got something wrong.
Once I've got hold of a value f开发者_如何学Gorom the XML, can I contain that value in a variable in such a way that functions outside the AJAX call are able to read it?
jquery selectors a are not meant for parsing the xml wrapping the xml
in jquery object and parsing it that way is browser dependent use .parseXML
instead
here is a demo http://jsfiddle.net/AKJwF/3/
$(document).ready(function(){
$('#header').click(function(){
$('p#test').html('At least <em>this</em> works');
$.ajax({
type: "GET",
url: "archives.xml",
datatype: "xml",
success: function(xml){
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$title = $xml.find( "title" ).text();
$('div#viewer').append($title);
}
});
});
});
精彩评论