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:
- jQuery.ajaxSetup({
- dataFilter: function(data, dataType) {
- if(dataType == "json") {
- var obj = jQuery.parseJSON(data);
- if(obj.meta.flexin.is_generic_json) {
- // We have a generic flexin JSON object, let's process generic stuff here...
-
- // return the actual json object...
- return obj.json;
- } else {
- // this is not a generic flexin JSON object, let just return the regular data...
- return data;
- }
- } else {
- // we do not have a JSON object - let's just don't do anything and return the data as is
- return data;
- }
- }
- });
On the server side, I have a generic methode "serveJson()" which does the following:
- $json = array();
- if(is_array($value)) {
- $json["json"] = json_encode(utf8_encode_array($value));
- } else {
- $json["json"] = json_encode($value);
- }
- // adding generic stuff here...
- // 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...
- $json["meta"]["flexin"]["is_generic_json"] = true;
-
- // TODO: add generic stuff here... (such as error messages...)
-
- echo json_encode($json);
- 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.