在JavaScript中使用Pylons全局变量(转义括号) [英] Using Pylons global variables with JavaScript (escaping brackets)

查看:169
本文介绍了在JavaScript中使用Pylons全局变量(转义括号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var selected = jQuery 

我试图在JavaScript中的Python全局变量中存储字典中的结果。 ('.target option:selected')。text()
var list =$ {c.persons_by_permission [+ selected +]}

如果我不使用变量直接访问字典:

  var list =$ {c.persons_by_permission ['stringID']}

然后代码有效。 / p>

然而,当我尝试使用变量来访问字典时,我得到一个语法错误(我相信它是因为大括号)。



我试过用大括号括起来:

  var list =$ {{c。 persons_by_permission [+ selected +]}}

但它提供了更多的语法例外。



有什么想法?

编辑:

这不是我是否可以一起使用Python和JavaScript的问题,我t更多的是摆脱大括号的问题。



我正在使用挂架,所以如果我尝试:

  alert($ {c.persons_by_permission ['Documents']})

我会得到我需要的结果。问题是我无法找到一种将变量传递到JavaScript中以代替'Documents'的方法。

解决方案

我是在这里做一些(合理的)假设......


  • 您的Python正在您的网络服务器上运行

  • JavaScript正在客户端上运行



就Python而言,它正在输出文本。它并不在意浏览器可能将其解释为HTML,JavaScript或其他。它只是生成一些文本,然后将其发送到浏览器。



使用HTTP,您可以发出请求并获得响应。您无法停止响应以从客户端获取数据。



您无法将客户端JS的数据传回给生成JS的Python进程。您可以:




  • 全部发送首先将数据发送到客户端(然后在JS中而不是在Python中选择所需的位)

  • 让JavaScript创建新的 HTTP请求到服务器并在JS中处理响应(这就是所谓的Ajax,并且此处不乏教程)


I am trying to access a result in a dictionary held in a Python global variable within JavaScript.

var selected = jQuery('.target option:selected').text()
var list = "${c.persons_by_permission["+selected+"]}"

If I directly access the dictionary without the using the variable:

var list = "${c.persons_by_permission['stringID']}"

Then the code works.

However, when I am trying to use the variable to access the dictionary I get a syntax error (I believe its because of the curly braces).

I have tried escaping the braces with:

var list = "${{c.persons_by_permission["+selected+"]}}"

But it gives more syntax exceptions.

Any ideas?

EDIT:

This is not an issue of whether I can use Python and javascript together, it is more of a question of escaping the curly brackets

I am using pylons, so if I try:

alert("${c.persons_by_permission['Documents']}")

I will get the results I need. The issue is that I cannot find a way of passing a variable into the javascript in place of 'Documents'

解决方案

I'm making a few (reasonable) assumptions here…

  • Your Python is running on your webserver
  • Your JavaScript is running on your client

As far as the Python is concerned it is outputting text. It doesn't care that the browser might interpret it as HTML, JavaScript or whatever. It just generates some text and then sends it to the browser.

With HTTP you get to make a request and have a response. You cannot stop a response half way through to get data back from the client.

You cannot pass data from client side JS back to the Python process that generated the JS. That process will have finished running.

You can either:

  • Send all the data to the client in the first place (and then select the bits you want in JS instead of in Python)
  • Have the JavaScript make a new HTTP request to the server and process the response in JS (this is known as Ajax and there are no shortage of tutorials out there for this)

这篇关于在JavaScript中使用Pylons全局变量(转义括号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆