/ Published in: ActionScript 3
Here is a basic example as to how to set up an Ajax request that will expect an XML return. For you code borrowers, you may have a hard time getting this to work with your own projects without understanding how JS, Ajax, and jQuery work.
Expand |
Embed | Plain Text
<style type="text/css" media="screen"> .ajaxLoaderCircle{ background: transparent url("http://www.ajaxload.info") top center no-repeat; margin-top: 1em; height: 3em; } </style> <!-- jQuery 1.3.1 Framework --> <script type="text/javascript" charset="utf-8" src="http://jqueryjs.googlecode.com/files/jquery-1.3.min.js"></script> <script type="text/javascript" charset="utf-8"> function getXML( url ){ $.ajax({ url: url, dataType: "xml", encoding:"UTF-8", beforeSend: xmlStart, success: xmlSuccess, error: xmlError, complete: xmlComplete }); } function xmlStart( xhrInstance ){ //Clear Any Previously Written HTML $( "#primary .content" ).html( "" ); //Show Preloader $( "#primary .content" ).addClass( "ajaxLoaderCSS" ); } function xmlError( xhrInstance, message, optional ){ $("#primary .content").html("<ul><li>There was an error loading the document.</li></ul>"); } function xmlComplete( xhrInstance, status ){ $( "#primary .content" ).removeClass( "ajaxLoaderCSS" ); } function xmlSuccess( data, status ){ //Write your Parse XML Function parseXML( data ); } function parseXML( xml ){ // XML Tag identifying each item var itemTag = "image"; // XML Tag giving the title var titleTag = "caption"; // XML Tag giving the link var urlTag = "uri"; // Var for storing the HTML to be displayed var content = "<ul>"; //Parse the XML and build new <li>'s for each NODE $( xml ).find( itemTag ).each(function(){ var title = $( this ).find( titleTag ).text(); var url = $( this ).find( urlTag ).text(); content += "<li><a href=\"" + url + "\">" + title + "</a></li>"; }); content += "</ul>"; // Populate the Tertiary div with the content trace( content ); } </script> <script type="text/javascript" charset="utf-8"> $(document).ready(function(){ getXML("http://x-y-z"); } </script>
Comments
Subscribe to comments
You need to login to post a comment.

pretty extensive for a snippet, and quite handy. It is in the wrong section though :)
the whole site is snippets, no other sections
hdragomir,
This is pretty extensive but hey, once you master this, AJAX will prove to be a piece of pie. This code should be easy enough to re-purpose for JSON & Plain Text Requests.