jQuery, Simple Ajax with .ajax()
If you have written some Ajax code using JavaScript you already know how much you have to write to accomplish the desired results. There's 10-15 lines of code to check for and validate the browser type (XMLHttpRequest, ActiveXObject) and all the error handling (try, catch) blocks. Then you have to verify the readyState Response values and hope you get a 4(Complete) so you can move on to manipulate and use the responseText. That's a whole lot of work, but made easy if you choose to use jQuery. What was done in a possible 40+ lines of code can be done in less that 1/2 the lines, not counting the actual jQuery Library code itself.There are 6 different ways to make an Ajax call using jQuery, but we're going to keep it simple with the most basic of them which is .ajax()
View the working example here.
Let's get an Ajax call working with jQuery.
$.ajax({
url: 'url',
dataType: 'xml',
success: function(data) {
// Manipulate the returned data here or elsewhere
}
});
That's pretty much all you need to start with. Now let's make it do something.
First thing we can do is putthe akax call into a ready function to be handled at the time of onload. And now let's add the url where the xml feed lives and write up the processing of the returned value "data" The naming of data could be any word you choose. To handle the feed I created a loop(each) to grab selected content from the feed before displaying it. In the loop the attribute "item" is every parent xml node that contains the content(child nodes) and then we process those item nodes and search for the content we are going to display. Then we write it to the document.
$(document).ready(
function(){
$.ajax({
url: 'sample.xml',
dataType: 'xml',
success: function(data) {
// Manipulate the returned data here or elsewhere
$(data).find('item').each(function(i) {
title = $(this).find('title').text();
description = $(this).find('description').text();
$('.box').append(title + "<br />" + description + "<br /><br />");
});
}
});
});
This is a very simple example and works pretty well. There are many other options and arguments you can add to the actual call to ajax() such as cache, error, complete, type and more. But this is all we needed to get started.
Try it here and view source to see the code if you like. I hope this helps.
You can view the simple and basic xml document here.
$(document).ready(
function(){
$.ajax({
url: 'sample.xml',
dataType: 'xml',
success: function(data) {
// Manipulate the returned data here or elsewhere
$(data).find('item').each(function(i) {
title = $(this).find('title').text();
description = $(this).find('description').text();
$('.box').append(title + "<br />" + description + "<br /><br />");
});
}
});
});
This is a very simple example and works pretty well. There are many other options and arguments you can add to the actual call to ajax() such as cache, error, complete, type and more. But this is all we needed to get started.
Try it here and view source to see the code if you like. I hope this helps.
You can view the simple and basic xml document here.
2whoa.com - NEW!!
Home
jQuery
Javascript
Archives
MLB Wallpapers
NFL Wallpapers
NBA Wallpapers
NHL Wallpapers
Flames Wallpaper
Watch Live Games!
NHL
NBA
Soccer
NFL
and more..
BMW, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Home
jQuery
Javascript
Archives
MLB Wallpapers
NFL Wallpapers
NBA Wallpapers
NHL Wallpapers
Flames Wallpaper
Watch Live Games!
NHL
NBA
Soccer
NFL
and more..
BMW, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan,Volvo Volvo Volvo Honda, Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota



Leave a comment
Volvo, Toyota, Volvo, Saab, Audi, Nissan Mercedes, Volvo, Nissan, Honda,Volvo Volvo Volvo Acura, Honda, Acura Forerunner, SUV, Volvo, Mercedes, Toyota