简体   繁体   English

如何将原始数据从控制器传递到javascript中?

[英]How to pass raw data from controller into javascript?

I want to pass certain data to a javascript function my_function(data) embedded in my view. 我想将某些data传递给嵌入在视图中的javascript函数my_function(data)

In the controller I do this: 在控制器中,我这样做:

data = "[{value: 1, color: '#FF0000'},{value: 1, color: '#FF0000'} ]"

That is the way the data has to come into the function. 这就是数据必须进入功能的方式。 I came a long way using this: 我用了很长一段路:

my_function({{ =XML(response.json(data)) }}

It works up to this point, but the data ends up like this: 至此工作正常,但数据最终如下所示:

"[{value: 1, color: '#FF0000'},{value: 1, color: '#FF0000'} ]"

but I kneed it like this - without the quotes around [...] : 但我这样屈膝-没有[...]的引号:

[{value: 1, color: '#FF0000'},{value: 1, color: '#FF0000'} ]

How do I manage to do so? 我该如何做呢?

data is already JSON, so no need for response.json -- just do: data已经是JSON,因此不需要response.json只需执行以下操作即可:

my_function({{=XML(data)}})

Alternatively, you can make data a Python object and use response.json to convert it to JSON: 另外,您可以将data作为Python对象,然后使用response.json将其转换为JSON:

data = [{'value': 1, 'color': '#FF0000'}, {'value': 1, 'color': '#FF0000'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM