dataFilter option - I'm beggining to loose my mind

dataFilter option - I'm beggining to loose my mind

Hi,

jQuery offers the ability to set a DATAFILTER for all AJAX calls.
I would LOVE to use this feature, so I tried implementing this...

This is the code on the client that I have:

  1. jQuery.ajaxSetup({
  2.         dataFilter: function(data, dataType) {
  3.             if(dataType == "json") {       
  4.                 var obj = jQuery.parseJSON(data);      
  5.                 if(obj.meta.flexin.is_generic_json) {
  6.                     // We have a generic flexin JSON object, let's process generic stuff here...
  7.                        
  8.                     // return the actual json object...
  9.                     return obj.json;
  10.                 } else {
  11.                     // this is not a generic flexin JSON object, let just return the regular data...
  12.                     return data;
  13.                 }
  14.             } else {
  15.                 // we do not have a JSON object - let's just don't do anything and return the data as is
  16.                 return data;
  17.             }   
  18.         }
  19.     });
On the server side, I have a generic methode "serveJson()" which does the following:

  1. $json = array();
  2. if(is_array($value)) {
  3.   $json["json"] = json_encode(utf8_encode_array($value));
  4. } else {
  5.   $json["json"] = json_encode($value);  
  6. }
  7. // adding generic stuff here...
  8. // this attribute is set so the client know json was served using this function, and we need to pre-process using the dataFilter option in jQuery...
  9. $json["meta"]["flexin"]["is_generic_json"] = true;
  10.    
  11. // TODO: add generic stuff here... (such as error messages...)
  12.  
  13. echo json_encode($json);
  14. exit();

As you can see, the obj.json will in fact contain a JSON string which I believe would be right to return for further processing by the code.  For some reason however, this breaks everything.

I already tried returning jQuery.parseJSON(obj.json) to force it to return a json object in stead (which would in fact surprise me), but it doesn't work either.

So I'm stuck here.  I believe that the dataFilter option should return "sanitised" data, being the data that my code used to receive before I wrapped it in an array containing additional metadata, but it appears I'm missing something here...

Hoping someone will be able to help me out here!

Thanks a lot!
David.