Mike, this was very educative post, thank you very much, it works now.
So the bottom line is that ajax call is asynchronous with the code that goes after ajax call (rest of the code is not waitng for the ajax call to finish) and that everything that depends on data returned from ajax call has to go inside $.getJSON(dataURL, function(json) Thanks once again. On Aug 1, 8:45 pm, "Michael Geary" <m...@mg.to> wrote: > Do you have any debugging tools such as Firebug? You don't have to spend > hours on a problem like this, when Firebug can show you the answer > instantly. > > Simply put this line of code before your eval(): > > console.log( json ); > > Then look at the Firebug console. You will see that your 'json' variable is > not a string. > > In fact, it is an array object. The $.getJSON() function does the eval() for > you, so by the time it calls your callback function, you already have the > JSON data as an object (or array, or whatever type the JSON string > represents). > > That's why the error message cites this code: > > items1=[object Object] > > Because 'json' is an object, when you write: > > "items1=" + json > > JavaScript converts the 'json' object back into a string, using that generic > '[object Object]' format. > > Since 'json' is already the array object you're looking for, instead of > using eval() you can simply do a normal variable assignment: > > items1 = json; > > With that fixed, though, I suspect you're about to run into another problem. > > Reading between the lines, I have a feeling you may be writing code like > this: > > var items1; > > $.getJSON(dataURL, function(json){ > items1 = json; > }); > > // Now do stuff with 'items1' here > > If so, that won't work. The code where that "Now do stuff..." comment > appears will run *before* the JSON data is downloaded, so the value of > 'items1' will be undefined. > > If you want to do something with the JSON data, you need to do it by calling > a function at the time that the $.getJSON callback is called: > > $.getJSON(dataURL, function(json){ > doStuff( json ); > doSomethingElse( json ); > andMoreStuff( json ); > }); > > Indeed, that's why $.getJSON uses a callback function in the first place - > because that's the only to insure that the JSON data is ready when that code > is run. > > -Mike > > > > > From: UK > > > Hello, > > > I'm trying to execute simple scenario, from the server side > > (servlet) I'm returning: out.print("[{\"type\":\"basic\"}]"); > > > on the client side I have : > > > var dataURL = "<%=response.encodeURL(request.getContextPath())%>/ > > FetchData"; > > > var items1; > > > $.getJSON(dataURL, function(json){ > > > eval("items1="+json); > > }); > > > FireFox is showing jscript exception in the console: > > > Error: missing ] after element list > > Source File:http://localhost:10038/wps/PA_1lb791mt//jsp/html/test.jsp > > Line: 345, Column: 15 > > Source Code: > > items1=[object Object] > > > Also tried with, but result was the same : > > items1 = eval("(" + eval(json) + ")"); > > > Lost several hours trying to solve this but no success. > > > Thanks- Hide quoted text - > > - Show quoted text -