Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript函数中访问python对象?_Javascript_Python_Cherrypy_Genshi - Fatal编程技术网

如何在javascript函数中访问python对象?

如何在javascript函数中访问python对象?,javascript,python,cherrypy,genshi,Javascript,Python,Cherrypy,Genshi,这意味着现在我正在使用CherryPy和genshi框架开发python,借助于此,我使用genshi将python变量发送到html文件,但我想在javascript函数中访问该变量,那么我该怎么做呢 html文件是: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:py="http:/

这意味着现在我正在使用CherryPy和genshi框架开发python,借助于此,我使用genshi将python变量发送到html文件,但我想在javascript函数中访问该变量,那么我该怎么做呢

html文件是:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:py="http://genshi.edgewall.org/">
<head>
    <title>Home</title>
    <script type="text/javascript">
        function setparent(val) {
            document.getElementById("tpar").value=val;
        }
    </script>
</head>
<body>
    <div >
        <table>
            <div py:for="user in users">
                <tr>
                    <td>
                        <div title="${user.userid}" onclick="setparent('${user.userid}')">${user.fname+" "+ user.lname}</div>
                        <py:if test="user.resource.__len__() > 0">
                            <div>
                                <py:for each="res in user.resource">
                                    <div><a href="">${user.resource[res]}</a></div>
                                </py:for>
                            </div>
                        </py:if>
                    </td>
                </tr>
            </div>
        </table>
    </div>
</body>
</html>

家
函数setparent(val){
document.getElementById(“tpar”).value=val;
}
${user.fname+“”+user.lname}
因为我想在javascriptfunction中访问py的“users”变量


你能告诉我吗?

将值从python传递到javascript的正确方法是——特别是如果它们可以包含不仅仅是普通数字或非常有限的字符集(基本上是
[a-zA-Z0-9]
和非引号符号)——通过json编码器(
json.dumps(…)
)运行值确保它们不会破坏任何东西。

请从示例中删除所有不必要的代码,只显示重要的内容。现在很难阅读。你的文章中包含的a不是a-这是错误的语法<代码>可能应替换为
。好的,谢谢,先生。。。。但我想将某个类的对象传递给jacascript。。在python中可能吗?您只需确保它可以序列化为json对象。有关详细信息,请参阅json模块的文档。