Ajax POST 挂了

Ajax POST is hanging

我有一个网络表单,它只是一组简单的文本区域,通过 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 开发工具中,我可以看到 POST 到 'save_teacher_actions_no_redirect.py' 发生了,我请求的页面的 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())

请指教!

我成功了。从本质上讲,这是因为我要发布的文件是 .py 而不是 .cgi...我的缺乏经验再次闪耀。