1

I am trying to save an array of objects sent from the server side in a client side var but for some reason the var is a just a long string of

"[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"

insted of the actual array.

my code is as follows

html.no-js(lang='en')
  //<![end if]
  head
    script(type='text/javascript')
        var x = '#{Countries}';
4

1 回答 1

2

您需要将它们编码为 JSON。

JavaScript:

JSON.stringify(Countries);

CoffeeScript:(我认为)

JSON.stringify Countries

如果您想将其转换回对象,请使用 JSON.parse,如下所示

JSON.parse(CountriesText);

(JSON 编码必须发生在服务器端 node.js 实例上)

于 2013-06-22T18:41:57.873 回答