【发布时间】:2016-02-23 11:32:34
【问题描述】:
我有一个网络表单,它只是一堆通过 CGI 将数据写入 SQL 数据库的简单文本区域。这很好用。
我的问题是,除了我必须提交表单的<input type='submit'/> 之外,我还希望在用户离开页面时提交它 - 为了实现这一点,我有以下 jQuery:
<script>
$(window).on('beforeunload', function(){{
$.ajax({{
type:'POST',
url:'../save_teacher_actions_no_redirect.py',
data:'group={group}&AP={AP}&year=14/15&' + $('#ta_form').serialize(),
success:function(){{}}
}});
}});
</script>
例如,在 Chrome 开发工具中,我可以看到“save_teacher_actions_no_redirect.py”的 POST 发生了,我请求确保此事件在导航时触发的页面的 GET 也发生了,但是这两个请求似乎都没有完成,并且页面只是挂在开发工具中的状态为(pending)
据我所知,ajax POST 正在调用的 .py 文件工作正常 - 日志记录不会引发任何错误或任何东西,而且绝对没有它陷入的循环或任何类似的东西,所以我不知所措为什么会挂起。
#!C:\Python34\python.exe
import datetime
import pymssql
import cgi
import os
from connection_data import NationalAverages
myDB = NationalAverages()
from pm_authenticate import authenticate_user_against_group
#logging
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler=logging.FileHandler('error_log.log')
logger.addHandler(handler)
# Connection details for the database
cursor = myDB.connect_to_db()
conn = myDB.conn
form = cgi.FieldStorage()
def right(string):
return string[len(string)-1]
def default_value(value):
if value is None:
return ' '
else:
return value
def left(string):
return string[0:3]
def save_data(form):
group = form.getfirst("group", "7bg/IL4")
AP = form.getfirst("AP", "AP2")
academic_year = form.getfirst("year", "14/15")
list_of_ids = []
# get ids of the rows on page
for key in form.keys():
if ((left(key) == 'ta_' or left(key) == 'da_') and right(key).isnumeric()):
if int(right(key)) in list_of_ids:
continue
else:
list_of_ids.append(int(right(key)))
else:
continue
cursor.execute('SELECT DISTINCT ID FROM Teacher_Actions WHERE Academic_Year = %s AND group_code = %s AND AP = %s', (academic_year, group, AP))
existing_ids = cursor.fetchall()
for id in existing_ids:
if id in list_of_ids:
continue
else:
cursor.execute('DELETE FROM Teacher_Actions WHERE ID = %d AND Academic_Year = %s AND group_code = %s AND AP = %s', (id, academic_year, group, AP))
conn.commit()
for id in list_of_ids:
cursor.execute('SELECT * FROM Teacher_Actions WHERE ID = %d AND Academic_Year = %s AND AP= %s AND group_code =%s', (id, academic_year, AP, group))
existing_data = cursor.fetchone()
if not existing_data:
cursor.execute('INSERT INTO Teacher_Actions VALUES (%d, %s, %s, %s, %s, %s, %s, %s)', (id, academic_year, group, AP, form.getvalue('ta_students_' + str(id)), form.getvalue('ta_gaps_' + str(id)), form.getvalue('ta_action_when_' + str(id)), form.getvalue('ta_success_point_' + str(id))))
conn.commit()
else:
cursor.execute('UPDATE Teacher_Actions SET Students = %s, Gaps = %s, Action_When = %s, Success_Point = %s WHERE ID = %d AND Academic_Year = %s AND Group_Code = %s AND AP = %s', (form.getvalue('ta_students_' + str(id)), form.getvalue('ta_gaps_' + str(id)), form.getvalue('ta_action_when_' + str(id)), form.getvalue('ta_success_point_' + str(id)), id, academic_year, group, AP))
conn.commit()
try:
save_data(form)
except:
logger.exception(datetime.datetime.now())
请指教!
【问题讨论】:
-
为什么在 ajax 调用和 beforeunload 函数周围都有两个大括号?
-
HTML 页面是由 CGI 调用生成的,该调用需要双括号来转义它们 - 单括号被解释为要填充的变量。
-
您说请求挂起,这意味着服务器端有问题。然而,您只发布您的客户端代码。
-
@jsfan - 添加了 .py 文件的代码。我有点假设它不是那个文件,因为在其他地方有一个几乎相同的文件可以正常工作,但我很高兴被证明是错误的!
-
您是否尝试确定您的服务器端脚本是否完成?在这种情况下,可能是您的 Web 服务器没有正确关闭连接,甚至可能永远缓冲输出。
标签: jquery python html ajax cgi