Ajax Jquery - Accumulating requests

2

Good morning everyone,

I have a very strange problem with a system I picked up for maintenance. All the requests I make to the server have been building up.

For example:
- I perform user exclusion 01. It sends the request and deletes the user without problems. - I delete the user 02. It sends the request to delete the user 01 and 02, the first one of the error (because the user does not exist anymore) and excludes user 02 without problems.

Has anyone ever had a similar problem, is it some configuration ??

I'm not very experienced in jquery, any tips already help me a lot.

Follow the code that makes the call

function confirmar(acao, elemento, campo_nome, campo_valor) {
$('.crud_msg').text(acao + " " + elemento + " com " + campo_nome + " igual " + campo_valor);
    $('#confirm_modal').modal('show');
    $('#confirm_modal').on('shown.bs.modal', function() {
        $('#confirm_modal_nbtn').focus();
    }).on('click', '#confirm_modal_ybtn', function(e) {
        $('#confirm_modal').modal('hide');
        $.ajax({
            url : '/apsig2/crud?action=' + acao + '&elemento=' + elemento + '&campo_nome=' + campo_nome + '&campo_valor=' + campo_valor,
            type : "POST",
            success : function(data, textStatus, jqXHR) {
                obj = $.parseJSON(data);
                if (obj.success) {
                    $('#user-table').bootstrapTable('refresh');
                    $('#action_success').modal('show');
                } else {
                    $('#action_failure').modal('show');
                }
                verifySession();
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                $('#action_failure').modal('show');
            }
        });
    });
}

Here is the code that generates the answer:

private void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.req = request;
        getElementAndAction();
        jsonResult = new JSONObject();
        JSONArray jsonResultArray = new JSONArray();
        String resultado = "";
        try {
            campoNome = requestGetParameter("campo_nome", req) != null ? requestGetParameter("campo_nome", req) : "";
            campoValor = requestGetParameter("campo_valor", req) != null ? requestGetParameter("campo_valor", req) : "";

            usuarioSoliciante = (Usuario)requestGetAttribute(USUARIO, request);

            switch (action) {
            case GET_TABLE:
                actionGetTable(jsonResultArray);
                resultado = jsonResultArray.toString();
                break;
            case EDITAR:
            case EDIT:
//              actionEdit();
                break;
            case EXCLUIR:
            case DELETAR:
                actionRemove();
                break;
            case REMOVER:
                actionRemove();
                break;
            case INSERT:
            case INSERIR:
            case PERSIST:
            case SAVE:
                actionInsert();
                break;
            }
        } catch (Exception e) {
            jsonResult.put(SUCCESS, false);
            JSONObject errors = new JSONObject();
            errors.put(REASON, e.getMessage());
            jsonResult.put(ERRORS, errors);
        }
        if (StringUtils.isBlank(resultado)) {
            resultado = jsonResult.toString();
        }
        if (StringUtils.isBlank(resultado)) {
            JSONObject errors = new JSONObject();
            jsonResult.put(SUCCESS, false);
            errors.put(REASON, "Nenhum resultado ao realizar a pesquisa.");
            jsonResult.put(ERRORS, errors);
        }
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.getWriter().print(resultado);
    }

Thank you

    
asked by anonymous 27.10.2015 / 14:19

2 answers

0

I solved the problem.

In fact every time the code below was executed it was not overwritten the "click" event but added another "click" event

 $('#confirm_modal').on('shown.bs.modal', function() {
        $('#confirm_modal_nbtn').focus();
    }).on('click', '#confirm_modal_ybtn', function(e) {
        $('#confirm_modal').modal('hide');
...

So they chained together ...

    
02.03.2018 / 20:34
0

The .on('click') command assigns a listener to the #confirm_modal_ybtn element each time the confirmar function is called. This way, when clicking the button a second time the same listener remains in the button and a new one is also assigned.

    
28.10.2015 / 03:22