[jQuery] Re: getJSON: use retrieved data outside the function???

2009-10-20 Thread Dave

Ok, thats how it work. Thanks for your answer. :)

Cheers / David

On Oct 20, 6:38 pm, MorningZ  wrote:
> "Why, and how can I make it work??? "
>
> Because you aren't understanding asynchronous behavior...
>
> The script doesn't "wait" for the $.getJSON call to finish before
> continuing on you'll need to do your work with "content" inside
> the success event, even if that means calling another function passing
> the json to it, like:
>
> var content = 'empty';
>             $.getJSON("getjson.json",
>                 function(json){
>                     content = json.layout.template[1].content;
>                     SomeFunction(content);
>                 });
>
> On Oct 20, 12:30 pm, Dave  wrote:
>
>
>
> > Hi
>
> > In the code below the second alert is executed before the first one
> > making it show the "empty" content, and not the result from the .json
> > file. Why, and how can I make it work???
>
> > -
> > getjson.html file
> > -
> >  > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> > http://www.w3.org/1999/xhtml";>
> > 
> >     getJson
> >     http://code.jquery.com/jquery-
> > latest.js">
> >     
> >         $(document).ready(function(){
> >             var content = 'empty';
> >             $.getJSON("getjson.json",
> >                 function(json){
> >                     content = json.layout.template[1].content;
> >                     alert(content);//This work
> >                 });
> >             alert(content); //This don't
> >         });
> >     
> > 
> > 
> > 
>
> > -
> > getjson.json file
> > -
> > {
> >         "layout":
> >         {
> >                 "template":
> >                 [
> >                         {
> >                                 "key": "01",
> >                                 "content": " > id=\"span1\">010101"
> >                         },
> >                         {
> >                                 "key": "02",
> >                                 "content": " > id=\"span2\">020202"
> >                         },
> >                         {
> >                                 "key": "03",
> >                                 "content": " > id=\"span3\">030303"
> >                         }
> >                 ]
> >         }
>
> > }


[jQuery] Re: getJSON: use retrieved data outside the function???

2009-10-20 Thread MorningZ

"Why, and how can I make it work??? "

Because you aren't understanding asynchronous behavior...

The script doesn't "wait" for the $.getJSON call to finish before
continuing on you'll need to do your work with "content" inside
the success event, even if that means calling another function passing
the json to it, like:

var content = 'empty';
$.getJSON("getjson.json",
function(json){
content = json.layout.template[1].content;
SomeFunction(content);
});


On Oct 20, 12:30 pm, Dave  wrote:
> Hi
>
> In the code below the second alert is executed before the first one
> making it show the "empty" content, and not the result from the .json
> file. Why, and how can I make it work???
>
> -
> getjson.html file
> -
>  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
> http://www.w3.org/1999/xhtml";>
> 
>     getJson
>     http://code.jquery.com/jquery-
> latest.js">
>     
>         $(document).ready(function(){
>             var content = 'empty';
>             $.getJSON("getjson.json",
>                 function(json){
>                     content = json.layout.template[1].content;
>                     alert(content);//This work
>                 });
>             alert(content); //This don't
>         });
>     
> 
> 
> 
>
> -
> getjson.json file
> -
> {
>         "layout":
>         {
>                 "template":
>                 [
>                         {
>                                 "key": "01",
>                                 "content": "010101"
>                         },
>                         {
>                                 "key": "02",
>                                 "content": "020202"
>                         },
>                         {
>                                 "key": "03",
>                                 "content": "030303"
>                         }
>                 ]
>         }
>
> }