Sunday, December 5, 2010

JQUERY AJAX with XML

Unfortunately can't use an example for this post at blogger.You know ajax needs the file to exist in the same domain. There are tricks for cross domain ajax but blogspot won't support to upload a file.

JQUERY has a very useful function .get() to fetch a file and render this. Let's take a look at the function.

I am using this code at localhost

$('#post6').click(function() {
$.get('atom.xml', function(data) {
$('#ajaxpost6').empty();
$(data).find('entry').each(function() {
var $entry = $(this);
var html = '
';
html += $entry.find('title').text();
html += '
';
$('#ajaxpost6').append(html);
});
});
return false;
});

when I click the post6 button it the get function returns a xmlHTTPrequest and if the request is successful it executes the second argument function.

And using the callback function I can render the data for my need. I can filter the data from the xml using the jquery's dom manipulation methods.

No comments:

Post a Comment