互斥提交

2024-06-26 13:49:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个javascript,它在一个“submit”事件上执行以下ajax调用(这反过来会触发一个python脚本),我现在的问题是“当一个submit事件发生时,如果其他人单击 提交按钮这个ajax调用应该通知提交正在进行”,有人遇到这个问题吗?(有名字吗?),如何解决此问题? 请建议。。你知道吗

$("#main_form").submit(function(event) {
       .....................

            $.ajax({
                dataType: "json",
                type: "POST",
                contentType: "application/json",//note the contentType definition
                url: "scripts/cherrypick.py",
                data: JSON.stringify(data_cp),
                //data: data_cp,
                error : function (xhr, ajaxOptions, thrownError){
                    console.log("cherypick fail");
                    console.log(response);      
                    console.log(response['returnArray']);
                    alert(xhr.status);
                    alert(thrownError); 
                },
                success: function(response){
                    console.log("cherypick sucess");
                    console.log(response);
                    console.log(response['returnArray']);
                    var return_array = response['returnArray'];
                    console.log(return_array['faillist'].length);
                    console.log(return_array['picklist'].length);       
                    for (var i = 0; i < ip_gerrits.length; ) {
                        for (var j = 0; j < return_array['faillist'].length; ) {
                            if (ip_gerrits[i] != return_array['faillist'][j] )
                                ipgerrits_pickuplist.push(ip_gerrits[i]);
                            j++;
                        }
                        i++;
                    }

Tags: iplogdatareturnresponsevarajaxfunction
2条回答

好的,只要您想同步所有用户的请求处理,就应该在服务器端完成。我假设您的服务器端是Python,即使您没有在问题中添加相关的标记。我的首选是C和PHP,但在您的情况下,我会做以下操作。。。你知道吗

选项#1-会话

1)为Python添加或安装更好的会话模块,人群建议使用烧杯

Python Module for Session Management

2)向服务器端脚本发送AJAX请求

$(form).submit(function(e) {

    var options = {
         url: "scripts/cherrypick.py"
    };

    $.ajax(options);

});

3)此服务器端脚本将包含类似以下代码

session_opts = {
    'session.type': 'file',
    'session.data_dir': './session/',
    'session.auto': True,
}

app = beaker.middleware.SessionMiddleware(bottle.app(), session_opts)

@hook('before_request')
def setup_request():
    request.session = request.environ['beaker.session']

@route('/cherrypick')
def index():
    if 'processing' in request.session:
        data = { 'procesing': request.session['processing'] }
        return data

    processor()

def processor():

    request.session['processing'] = 1

    # Do some processing here for the first request
    # When processing is done you can clear "state" variable in session

    del request.session['processing']
    request.session.modified = True

4)现在在JS脚本中,如果您得到包含键“processing”的JSON,您可能会向用户显示警报,提示他需要等待,直到第一个请求得到处理

选项#2-长轮询和Comet

对这个选项的描述可能需要更多的空间来描述,因此最好看一下本文,它有一个非常好的、干净的示例和Python中长轮询的实现

http://blog.oddbit.com/2013/11/23/long-polling-with-ja/

这里的主要思想不是保持静态会话,而是使用无限循环,它可以根据某些状态变量发回不同的HTTP响应:

@route('/cherrypick')
def index():

    while True :

        response = { 'processing': processing }
        print response

        if processing != 1 :
            processing = 1
            # Do some processing
            processing = 0

        sleep(5)

最简单的方法是关闭指示正在进行某些处理的标志:

var processing = false;   
$("#main_form").submit(function(event) {
    if (processing) {
        $("#some_notification_pane").text("hold on there, turbo!");
        return;
    }
    processing = true;
    ...
    $.ajax({
        ...
        error: function(xhr, ajaxOptions, thrownError) {
            ...
            processing = false;
        },
        success: function(response) {
            ...
            processing = false;
        }
    });
    ...
});

您可能还想禁用submit处理程序开头的submit按钮(这里有processing = true),并在收到响应后重新启用它。你知道吗

相关问题 更多 >