MXAJAX - mxData tag basics

The MXAJAX mxData tag is for those using MXAJAX who want complete control over the data (for integration with their own JS controls etc). The tag calls a CFC method or CF function (depending on your setup) and returns JSON corresponding to the CF return type.  In a future post I'll discuss each return type, but for now I'm going to demonstrate how to get at the info as the online example doesn't make it clear.

To parse JSON I recommend using the parser (strangely enough) rather that just evaluating it. It's all packaged with MXAJAX, so you can just go ahead in include it in your page.  Once it's been parsed, you can do with it what you want; the example below (a slightly modified version of the official mxData 1 example) writes the return into a span:

<html>
    <head>
        <title>Returning Data from CF page</title>
        <script type='text/javascript' src='../core/js/prototype.js'></script>
        <script type='text/javascript' src='../core/js/mxAjax.js'></script>
        <script type='text/javascript' src='../core/js/mxData.js'></script>
        <script type='text/javascript' src='../core/js/json.js'></script>
        <script language="javascript">
            var url = "<cfoutput>#ajaxUrl#</cfoutput>";
           
            function init() {
                new mxAjax.Data({
                    executeOnLoad:true,
                    paramArgs: new mxAjax.Param(url,{param:"id=1", cffunction:"getContent"}),
                    postFunction: handleData
                });
               
                function handleData(response) {
                    var myHTMLOutput = JSON.parse(response);
                    document.getElementById("myTargetSpan").innerHTML = myHTMLOutput;
                }
            }
           
            addOnLoadEvent(function() {init();});
        </script>
    </head>
    <body>
        <h1>Returning Data from CF page</h1>
        <span id="myTargetSpan"></span>
    </body>
</html>

The next post will be a more complicated example, doing something with a CF query.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
saloni's Gravatar In this example we are saving the result using cfsavecontent and passing it back to .cfm page. Now if i want to create a structure from the resultset
and pass this structure to .cfm page and display its values there how will it be done?
# Posted By saloni | 11/22/06 7:49 AM
James Holmes's Gravatar Same process - return the struct, parse the JSON and use the results as necessary. You might want to try DP_Debug to see the structure of the returned data.
# Posted By James Holmes | 11/24/06 11:35 PM
Will Tomlinson's Gravatar James,

Would you please email me regarding this example at:

wt <AT> wtomlinson.com

Thanks much,
Will
# Posted By Will Tomlinson | 6/12/07 10:03 AM
Will Tomlinson's Gravatar Check this out:

http://www.techscreencast.com/screencast/mxajax_mx...

He shows you how to do that.
# Posted By Will Tomlinson | 6/12/07 2:53 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.5.1.