views:

66

answers:

2

I'm using JSON to communicate some data through AJAX from the backend to the frontend (Javascript, of course). The XMLHttpRequest is done by a Prototypejs-written AJAX-handler (I'm still in the process of migrating to jQuery, but the noConflict-mode allows me to run both simultaneously), after which PHP sends the X-Json header with some data like objects = {'foo': 'bar'}.

The Prototypejs-Ajax.Request passes a json variable to the onSuccess-callback, containing all the JSONdata, so the processing isn't the hard part. However, I noticed that since the JSON is automatically evaluated, the objects variable is made global (and thus a member of the window object.

I know it's not really a problem since it's just an unused variable, but I always tried to stay away from global variables as much as possible. Additionally, the datasets may be pretty large on some occasions, so it'll just be a huge duplicate variable of which one is never used. So, if possible, I'd rather lose the global.

My question: how? If there's a good reason for this happening, or if this is just a Prototypejs-specific issue, or if this just indicates I'm doing something very wrong, please tell me :-)

Thanks!

+2  A: 

Are you sending back objects = {"foo":"bar"} from PHP? When sending JSON, you just send {"foo":"bar"} and get the data as the return result of eval:

var json = '{"foo":"bar"}'; // This string would really be coming from PHP
// ...
var objects = eval('(' + json + ')'); // objects variable will be limited to the
                                      // current scope.

If, for some reason, you must evaluate objects = ..., you can limit the variable to the current scope before running eval:

var objects;
eval('objects = {"foo":"bar"}');


Note that the PHP functions json_encode and json_decode will create "proper" JSON for you, which means it will use double quotes, and it will not use any kind of assignment (True JSON is limited to an array/object as the outer-most value, and it may not contain assignment or function calls. See the JSON specification.)

Blixt
Thanks, I guess I was a little mixed up on the correct syntax. I think I remember writing it this way because I couldn't get Prototype to pass me the `json`-parameter, so I needed the auto-evaluation... Had something to do with a malformed X-Json header. This'll probably solve all the trouble, thanks ;-)
JorenB
+1  A: 

If the PHP is outputting objects = {'foo': 'bar'} then it isn't outputting JSON. JSON can't include the '=' character outside a string and uses the double quote character not the single quote. This means it is outputting JavaScript.

You have two options here.

The first is to try to restrict the scope of the JavaScript, the second is to fix the PHP so it outputs real JSON.

I would recommend the second option. Deal with JSON not JavaScript.

If you want to try to restrict it, then you need to limit the scope somehow. Possibly with:

function () {
    eval("var " + php_generated_code);
    doSomethingWith(object);
}

… although I'm not sure what the scope of eval() is.

David Dorward