我有一个我想异步执行的 python 任务(我尝试同步但它冻结了页面并Synchronous XMLHttpRequest on the main thread is deprecated
在控制台中给出警告)。
问题是我无法从文档中了解如何正确设置 ajax 调用以使代码在后台运行并取回数据。我目前正在使用 URL 来传递数据,但可能有一个更简单的解决方案(我不介意一切都可以在主 html 上完成)。
这是我的文件(示意图):
html
<body onload="brython()">
<input type="text" id="val" placeholder="My value here">
<button id="ok">Launch</button>
<div id="rez">The place where I want the result</div>
<!-- Calling my xyz.py file -->
<script type="text/python" id="script3">
# Here I try to to make an ajax async call to my xyz.py file,
# Currently the following code returns my xyz.py code in the div instead of the result of the operation
from browser import document, ajax
def on_complete(req):
data = req.responseText
document['rez'].text = data
def launch(e):
req = ajax.ajax()
input_data = document['val'].value
url = "./xyz.py?val="+input_data
req.bind('complete', on_complete)
req.open('GET', url, True)
req.send()
document['ok'].bind('click', launch)
</script>
</script>
</body>
蟒蛇(xyz.py)
from browser import document, ajax
#here I'd like to get the POSTed value, I think this should work
value = document.query['val']
def main(e):
#My main function, that runs for 30s and needs to be done asynchronously
#Reads from a big file and search regexs among other things
return result
document <= main(value)
#How to properly send back the result into my #rez div?
提前感谢您的任何帮助/建议!