Ajax Jquery - Accumulating requests

Asked

Viewed 196 times

2

Good morning to all,

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

For example:
- carry out user deletion 01. It sends the request and deletes the user smoothly.
- I carry out the exclusion of the user 02. It sends the deletion request of user 01 and 02, the first of the error (pq the user no longer exists) and deletes user 02 without problems.
- I do the deletion of user 03. It sends the user deletion request 01, 02 and 03, the first and second give errors (because users no longer exist) and deletes user 03 without problems.

Someone’s been through a similar problem, it’s some configuration??

I’m not very experienced in jquery, any hint would 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');
            }
        });
    });
}

Follow 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

  • 3

    The requests are asynchronous using AJAX, the first one you ordered "can" be executed last, but that’s weird. Can share your AJAX call code?

  • So answer the question, please. Then designate your answer as correct to close the question.

2 answers

0

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

0


I managed to solve the problem.

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

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

That’s why they bonded...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.