JQuery get $.getJSON data

27 June, 2009

Imagine you have server side data return to your Javascript client side using Ajax and JSON as datatype return.

var groupid = null; //try outer scope
$.getJSON(url, data, function(result){

    groupid = result.groupid;  //json type data
    //do whatehever you like here
    //you wanted to return the data that you have been parsed or assign

});
alert(groupid); //error : will return null because the JSON already happern after the callback executed.

So instead doing that, we need to use delegate or callback function that JQuery have provided to us.
Remember the $.fn. thing.

Now, let see how we can use the delegate function to return us the data:

$.getJSON(path +'?request=url', function( data ) {

var selectOption = '';
//do what ever you like with the json data

selectOption =  selectOption + 'testing' + data; //example only
$.fn.delegateJSONResult( selectOption ); 

} ); 

$.fn.delegateJSONResult = function{  alert( data );  }

With JQuery delegates, it makes your life easier as developer and the most common used to all my event handling.

To understand better about delegates, look at JQuery code to see how it actually works behind the background or alternatively try to play around with this sanbox.

The above source code highlighting was proudly developed by WordPress. Many thanks.

Entry Filed under: Javascript, jQuery. Tags: , , .

1 Comment Add your own

  • 1. RRaveen  |  3 July, 2009 at 12:56 AM

    Dear Friends,

    I hope you are doing well. I have launched a web site http://www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.

    Please forward this email to all of your friends who are related IT. Send to us your feed about site also.

    Thank you
    RRaveen
    Founder CodeGain.com

    Reply

Leave a Comment

hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Charles Ling

Founder of ecomjunction.com

My Favourite Web

 

June 2009
M T W T F S S
« May   Jul »
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

Tags

Adobe bitcomet book coding Firefox Intelligent Intelligent Web J2ME Java javascript javascript framework jQuery Jsp jsr 172 kite runner LWUIT Machine Learning Mysql novel Nuance Talks painted veil parse Personalization Photoshop PHP plug-in privacy programming protocol buffer proxy Security server tcp Technology Tomcat web web 2.0 Web Service web services wep Wireless WSDL wsf wso2 xml

Archives

Delicious Bookmark

Join ME