Método JQuery Ajax llamado dos veces
Frecuentes
Visto 2,365 veces
0
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?
2 Respuestas
1
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
0
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 jquery ajax struts2 or haz tu propia pregunta.
is this Jquery function also getting called twice? - Umesh Awasthi
Seems like than the action is also getting called from some where else also. - Umesh Awasthi
@UmeshAwasthi The action is not called by any code elsewhere. I have tried returning
return false;
de$.getJSON
call, but still the same issue. - Uchenna NwanyanwuIts only possible in one case when action is getting triggered twice.If you remove the json call what is the bahaviour? - Umesh Awasthi
@UmeshAwasthi If i remove the JSON call, it is called once. - Uchenna Nwanyanwu