Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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 Flask通过url_for()向python发送变量数据_Javascript_Flask_Url For - Fatal编程技术网

Javascript Flask通过url_for()向python发送变量数据

Javascript Flask通过url_for()向python发送变量数据,javascript,flask,url-for,Javascript,Flask,Url For,我想通过url_for()从flask html/js向python发送一个变量值 Python代码: @app.route('/video_feed/<device>') def video_feed(device): # return the response generated along with the specific media # type (mime type) print(device) try: return Re

我想通过url_for()从flask html/js向python发送一个变量值

Python代码:

@app.route('/video_feed/<device>')
def video_feed(device):
    # return the response generated along with the specific media
    # type (mime type)
    print(device)
    try:
        return Response(gen(int(device)),mimetype = "multipart/x-mixed-replace; boundary=frame")
    except Exception as e:
        return Response(gen(device),mimetype = "multipart/x-mixed-replace; boundary=frame")

但它不起作用,我得到一个错误:

jinja2.exceptions.UndefinedError: 'data' is undefined
这确实有效,但是我不能在url_for()中使用变量data.value:

我尝试过几种不同的方法,如:

image.src = "{{ url_for('video_feed', device=${data.value})}}";
image.src = "{{ url_for('video_feed', device=$data.value)}}";
image.src = "{{ url_for('video_feed', device=%s)}}", data.value;
但似乎什么都不管用。我的javascript有点生锈了

任何帮助都将不胜感激


干杯

url\u的
在页面发送到浏览器之前以及JavaScript运行之前在服务器端展开

如果您在页面生成时知道编号(对于
设备
/
数据.value
),请通过
render\u template()
将其传递给模板


但是,如果在页面呈现之后才知道值,则需要从JavaScript构建
img
元素。

谢谢您的回复。在呈现页面之前,我不知道该值。这是我的设想。Flask网站通过SocketIO接收JSON格式的消息。上面的“data”变量就是那个JSON对象。字段“value”包含可捕获设备的地址。对于每个传入的JSON对象,我想为(…)创建一个src=url_的新对象。正如我所提到的,当我不使用变量时,这是有效的。我同意这是因为页面呈现的时间。我可以用别的方法吗?我有一些想法,比如,ajax?或者代理服务器或重构来解决python端的问题。哦,现在我明白你的意思了,url_for()生成的src可以在js中复制。所以我真的不需要使用url_for()。应该只使用
image.src=“/video\u feed/”+data.value
image.src = "{{ url_for('video_feed', device=0)}}";
image.src = "{{ url_for('video_feed', device=${data.value})}}";
image.src = "{{ url_for('video_feed', device=$data.value)}}";
image.src = "{{ url_for('video_feed', device=%s)}}", data.value;