views:

93

answers:

1

I would like to serialize an instance of JS object from the Server-side to the Client side (the object contains data members and functions)

I have a Javascript stack on both end, all my users use Chrome and My server side is a NodeJS impl..

how would I do it? It should be trivial as my server is a Javascript one..

+5  A: 

You could send it as JSON string. Here's an example:

response.writeHead(200, {'Content-Type': 'application/json'});
response.write(JSON.stringify(yourObject));
response.close();
Darin Dimitrov
What code should I use on the client side?
Have you tried Ajax? Note that client side javascript knows nothing about server side language. So traditional techniques should be used on the client side.
Darin Dimitrov
of course I would use AJAX, but I do I convert the String to an Object?
Well, there's `eval` but if you use a javascript framework such as jQuery it will automatically give you a js object in the AJAX `success` callback. For example: `$.post('/foo', function(obj) { /** obj will be directly an object here **/ });`
Darin Dimitrov