0

I like translate my json-Data to an JS object or assign it to a variable..

$.ajaxSetup({
type: "POST",
url: "_class/queries.php",
dataType:"json"
});

var obj;    

$.ajax({
   data: querystring,
   success: function(data){

     console.log(data);

     alert(data[0].vname);

     //obj = JSON.parse(JSON.stringify(data));

     obj = data;

     console.log(obj[0].vname);//<-- prints the expected property 
   }

 });

console.log("return "+obj);//<-- undefined?
return obj;

}

the alert print out the right property-value but somehow the eval-function produces an pars error? I've tryed jQuery.parseJSON but wont work either?

Don
  • 181
  • 1
  • 4
  • 12
  • 1
    Please show us the JSON data. Also, ajax is asynchronous, so on `return obj`, it might not yet be set. – Grant Thomas Jun 19 '11 at 17:28
  • Is your json valid? $.parseJSON will work if it is. Id avoid eval because of preformance issues. – locrizak Jun 19 '11 at 17:31
  • @locrizak it should be valid? [{"ID":"21","vname":"sad","name":"Höbert","adresse":"sdfdsf","plz":"34534","ort":"dfgdsfg","email":"sdfgsdfg"}] – Don Jun 19 '11 at 18:04

2 Answers2

0

The json variable contains already parsed value - the data returned by the server. Why do you you need to eval it?

obj = data;

is enough.

c-smile
  • 26,734
  • 7
  • 59
  • 86
0

Set the type property of $.ajax() to json:

$.ajax({
    data: querystring,
    dataType: "jsonp",
    success: function(json) {
        console.log(json);
        alert(json[0].vname);
        obj = eval("(" + json + ")");
    }
});

From the manual:

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

eval() shouldn't be used if possible - in your case, unless your JSON data contains actual Javascript code to execute you don't need to eval() it.

Also, the AJAX call is asynchronous - this means that the AJAX request is made out-of-order of usual program execution. In your code that means at return obj; the obj variable doesn't have the same value as it will after the AJAX call has returned with data from the server and assigned the return of the eval() call to obj. The solution to this is event-driven programming - in your case, have the success function call the next "event" that you'd like to occur, or use the AJAX function to populate a variable (in the global scope, preferably namespaced) that another event polls for (using a timing event such as setInterval()).

Community
  • 1
  • 1
Andy
  • 17,423
  • 9
  • 52
  • 69
  • thx, maybe that explains why everthing within the success-function works well even obj = data; works! But how can I return obj?? – Don Jun 19 '11 at 18:26
  • You must use event-based programming, see last paragraph above. – Andy Jun 19 '11 at 19:30
  • one solution is to put async:false as parameter of the $.ajax() – thx for the hints :) – Don Jun 20 '11 at 01:54