Re: EJS passing JSON to client

3,272 views
Skip to first unread message

greelgorke

unread,
May 17, 2013, 2:31:28 AM5/17/13
to expre...@googlegroups.com
<%= json %> just calls json.toString() that by default just calls the Object#toString with prints the class-string of the object.

what exactly do you do with your object? to clarify names first:

1. there is a JSON object, that can parse json strings and create json strings
2. JSON is a serialization format: javascript object notation
3. There are object literals in java script. JSON is inspired by them, but they are not json

So, do you want to just pass a Javascript object as JSON to your client, as data, not as page? then use http://expressjs.com/api.html#res.json
If you want to present your javascript object in some kind of HTML structure, then do html and access the properties if your object.

Am Freitag, 17. Mai 2013 02:52:07 UTC+2 schrieb Jake Isaac:
Hi

I have a question about retrieving a JSON object back from ejs.  I think I understand the problem but I don't know how to solve it.  I am passing JSON to the client, on the server it is in literal form but on the JSON side it displays as [object:object] when I do <%= json %>, I have tried using <%- json %> but I don't think it works I don't see any difference.  Here is the code snippet if it helps:

index.js:
res.render('maps', {
    title: "Title ",
json: json
 });

maps.html:
<%- json %>

If I do json.forEach.... I can iterate over the values, so I know data is there.

thanks in advance
Jake

Jake Isaac

unread,
May 17, 2013, 9:48:57 AM5/17/13
to expre...@googlegroups.com
Thanks for your response.  What the page is getting is number 2 but I think I want 3.  I was trying to pass json to my client side JS load to pin cushions onto a map

...
var mapJson = <%- json %>;
console.log(mapJson);
for (var i = 0; i < mapJson.length; i++) {
...

maybe there is a better way to do this, I just don't know how.  I'll read through through that link you sent.

thanks

greelgorke

unread,
May 17, 2013, 2:31:39 PM5/17/13
to expre...@googlegroups.com
on client side you did an Ajax request to your server and expects just the data object? then you should definitely do res.json() on teh server. and you have probably parse the response on the client, that depends on wich library you're using there

Martin Wawrusch

unread,
May 17, 2013, 2:34:06 PM5/17/13
to expre...@googlegroups.com
Assuming you want to embed the json as part of the document you just need to do a JSON.stringify(yourDataObject).


--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 




Reply all
Reply to author
Forward
0 new messages