Monday, 3 September 2012

Load XML / JSON files in JQuery

I have posted code snippets of loading XML and JSON files in Jquery as a quick reference. Loading XML can be written using the ajax() function or the shorten get() function. I will firstly provide code for loading XML using the ajax() function.

$(document).ready(function() 
{
 $.ajax({
        type: "GET",
        url: "xmlFileName.xml",
        dataType: "xml",
        success: parseXml
    });
    

 function parseXml(xml)
 { 
 
 
 }

});

The shorten way of loading XML with the Jquery is using the get() function.

$(document).ready(function() 
{
 $.get('xmlfilename.xml', function(data) {
    alert('xml loaded.');
 });

});

Loading JSON files is almost exactly the same as loading XML. You can load JSON using the Jquery's ajax() function or the getJSON().

$(document).ready(function() 
{
    $.ajax({
        type: "GET",
        url: "jsonFileName.json",
        dataType: "json",
        success: parseJSON
    });
    

 function parseJSON(json)
 { 
 
 
 }

});

The shorten way of loading JSON files is using the getJSON()function.
$(document).ready(function() 
{

 $.getJSON('jsonFileName.json', function(data) {
  
 });

});

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP