javascript - Mutually exclusive submits -
i have javascript on "submit" event following ajax call(which in turn triggers python script),my problem "when 1 submit event going on if else clicks on submit button ajax call should notify submission in progress" ,has ran problem?(is there name?) ,how fix problem? please suggest..
$("#main_form").submit(function(event) { ..................... $.ajax({ datatype: "json", type: "post", contenttype: "application/json",//note 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); (var = 0; < ip_gerrits.length; ) { (var j = 0; j < return_array['faillist'].length; ) { if (ip_gerrits[i] != return_array['faillist'][j] ) ipgerrits_pickuplist.push(ip_gerrits[i]); j++; } i++; }
ok, far want synchronize requests processing users, should done on server side. assume server side python, though did not add relevant tag question. preferences c# , php, in case following ...
options # 1 - session
1) add or install preferable session module python, crowd recommends use beaker
python module session management
2) send ajax request server side script
$(form).submit(function(e) { var options = { url: "scripts/cherrypick.py" }; $.ajax(options); });
3) server side script have code
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 # processing here first request # when processing done can clear "state" variable in session del request.session['processing'] request.session.modified = true
- bottle.py session beaker
- http://beaker.readthedocs.org/en/latest/sessions.html#using
- http://flask.pocoo.org/snippets/61/
4) in js script if json contains key "processing" may show alert user needs wait until first request processed
option # 2 - long polling , comet
description of option may take more space describe, better @ article, has quite nice , clean example , implementation of long polling in python
http://blog.oddbit.com/2013/11/23/long-polling-with-ja/
the main idea here not keep static session use infinite loop instead can send different http responses depending on state variable :
@route('/cherrypick') def index(): while true : response = { 'processing': processing } print response if processing != 1 : processing = 1 # processing processing = 0 sleep(5)
Comments
Post a Comment