1 回答
TA贡献2080条经验 获得超4个赞
您可以通过在指向呈现空 HTTP 响应的 URL 下添加 AJAX 函数来完成此操作view.html:URL /run_ajax(应包含在您的 中urls.py)应如下所示:
```re_path(r'^run_ajax', views.function_to_call_with_ajax)```
function_to_call_with_ajax 是这样的(在 view.py 中):
def wait_and_print(request):
print(5)
time.sleep(90)
return HttpResponse("")
AJAX 功能:
<script type="text/javascript">
$( document ).ready(function() {
console.log("ready!");
$.ajax({
url: '/run_ajax', //The URL you defined in urls.py
success: function (data) {
console.log('data ', data)
//If you wish you can do additional data manipulation here.
},
error: function (error) {
console.log('error', error)
}
});
添加回答
举报