[英]How to assign a python variable's value(string) as the value in JavaScript variable
I want to assign the email and password value got from the previous page through Python-CGI to JavaScript variable.我想将通过 Python-CGI 从上一页获得的电子邮件和密码值分配给 JavaScript 变量。 When I am doing the following code I am getting the below error,
当我执行以下代码时,出现以下错误,
CODE:代码:
#!/usr/bin/python3
import cgi
import cgitb; cgitb.enable()
print ("Content-type:text/html\n\n")
form = cgi.FieldStorage()
email=str(form.getvalue("email")[0])
password=str(form.getvalue("password")[0])
print (email,password) # email@email.com,123456
print ("""
<!DOCTYPE html>
<html>
<body>
<script>
var email=String("""+str(email.strip())+""");
var password=String("""+str(password.strip())+""");
alert(email);
alert(password);
</script>
</body>
</html>
""")
ERROR :错误:
Uncaught SyntaxError: missing ) after argument list参数列表后未捕获的 SyntaxError: missing )
Try to use f-string尝试使用f-string
email = 'jack@jack.com'
password = 'secret'
val1 = 133
html = f"""
<!DOCTYPE html>
<html>
<body>
<script>
var email='{email.strip()}';
var password='{password.strip()}';
var data={{'key1':{val1}}};
alert(email);
alert(password);
</script>
</body>
</html>
"""
print(html)
output输出
<!DOCTYPE html>
<html>
<body>
<script>
var email='jack@jack.com';
var password='secret';
var data={'key1':133};
alert(email);
alert(password);
</script>
</body>
</html>
you have to add quoet/double quote before and after the user input: because in the end , without adding the '' ,the code will be like this String(email@gmail.com);您必须在用户输入前后添加引号/双引号:因为最后,如果不添加 '' ,代码将像这样 String(email@gmail.com); so you have to add a quoet/double quote
所以你必须添加一个引号/双引号
#!/usr/bin/python3
import cgi
import cgitb; cgitb.enable()
print ("Content-type:text/html\n\n")
form = cgi.FieldStorage()
email=str(form.getvalue("email")[0])
password=str(form.getvalue("password")[0])
print (email,password) # email@email.com,123456
print ("""
<!DOCTYPE html>
<html>
<body>
<script>
var email=String('"""+str(email.strip())+"""');
var password=String('"""+str(password.strip())+"""');
alert(email);
alert(password);
</script>
</body>
</html>
""")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.