rajeev rajeev - 1 year ago 86
jQuery Question

using dataFilter from jquery .ajax with json data

how can i produce a modified vaild JSON data string from the JSON data coming from the ajax call. it is necessary to do this with dataFilter.

retData (data returned) from ajax call is:


JS code:

'dataFilter': function (retData, json) {
data = [];
$.map(retData, function (value,key) {
console.log('indx=i'+key+', value='+value);
node = {
'id' : 'i'+key,
'text' : value,
'icon' : '/',
'data' : value, //'metadata' : value,
'state' : {'opened' : false},
'children' : true
data.push( node );
return data;
//return JSON.stringify(data);

but instead i am getting error in browser console: it seems $.map is having the issue when run in 'dataFilter', it runs fine in 'success' call (but plugin strictly wants processing to be done in 'dataFilter')

TypeError: invalid 'in' operand a
Line 2

Answer Source

The dataFilter's arguments are both strings per the docs. You need to convert retData to an object before you can call $.map on it:

  'dataFilter': function(retData, json) {
    data = [];
    $.map(JSON.parse(retData), function(value, key) {
      console.log('indx=i' + key + ', value=' + value);
      node = {
        'id': 'i' + key,
        'text': value,
        'icon': '/',
        'data': value, //'metadata' :  value,
        'state': {
          'opened': false
        'children': true
    console.log('final-data=' + JSON.stringify(data));
    return data;
    //return JSON.stringify(data);