Método JQuery Ajax llamado dos veces

I have this function in a jsp which calls a struts2 action. But it calls it twice. Why?

JSP

    $("#inputField").change(function()
    {
        var inputField= $("#inputField").val();
        if(inputField !== '')
        {
            var url = 'myUrl';
            $.getJSON(url, function(retVal)
            {
                $.each(retVal.retList, function(index, element)
                {
                    console.log(element.name);
                });  
            });
        }
    });

Struts2 Action

public String getJSON() throws Exception
{
     System.out.println("Method Executed");
}

Cuando esto ajax call returns, i get this

Method Executed
Method Executed

How can i make this execute only once?

preguntado el 28 de agosto de 12 a las 08:08

is this Jquery function also getting called twice? -

Seems like than the action is also getting called from some where else also. -

@UmeshAwasthi The action is not called by any code elsewhere. I have tried returning return false; de $.getJSON call, but still the same issue. -

Its only possible in one case when action is getting triggered twice.If you remove the json call what is the bahaviour? -

@UmeshAwasthi If i remove the JSON call, it is called once. -

2 Respuestas

You might using JSON plugin and it's calling all your methods that start with "get" in an attempt to serialize them for output.Try to rename your method name

Respondido el 26 de Septiembre de 13 a las 08:09

Make sure that you only register the the 'change' event once. The reason for the double ajax call is probably that you are adding a change listener twice in the code. Take a look aquí.

contestado el 23 de mayo de 17 a las 11:05

It is only attached once, though the page is reloaded sometimes and i believe all events get unbound when a page reloads using window.location.replace(url);. So this means the event is bound only once. Correct me if i am wrong. - Uchenna Nwanyanwu

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.