I have already get a serier of json data from facebook using node js express. Now I want to send the data to a html page. But I have no idea how to send the data and encode the data in html.
3 Answers
If you use Express in node.js, here is the way to send json object as response:
app.get('/test', function(req, res, next) {
res.json({ message: 'Hello World' });
});
Then in JS on your html page html, if you use jQuery:
$.ajax({
url: '/test',
complete: function(data) {
console.log(data);
}
});
Feel free to experiment with stuff, as well use Dev Tools in Chrome, experimentation - helps a lot to understand what and how it works.
5 Comments
Bojangles
TIL
res.json() - that's just saved me a load of effort with JSON.stringify all over the placemoka
You could do res.send() as well, as far as I am concern, latest express will add application/json Content-Type if you try to send object. It is up to jQuery to parse it properly. Although if Content-Type is not json but you still sure it will be always json, then you can force it in jQuery by adding
dataType: 'json' in parameters in $.ajax({ ..Bojangles
Interesting, thanks for the info (although admittedly I feel like I should've just read the docs and saved you the time
;))moka
@JianyuanMa you are welcome. Please use accept features and rating system of StackOverflow, read this: stackoverflow.com/about
Marek Bernád
Finally, this is what I was looking for ... well working
Check out Heroku's example tutorial. I think it does exactly what you are asking for in terms of JSON encoding + throws in Web Sockets. https://devcenter.heroku.com/articles/node-websockets
1 Comment
songyuanyao
It is better to give a brief explanation, and provide the link for reference of details. Link-only answers may become invalid if the linked page changes.