简体   繁体   English

jQuery Ajax方法调用两次

[英]JQuery Ajax method called twice

I have this function in a jsp which calls a struts2 action. 我在一个调用struts2动作的jsp中具有此功能。 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");
}

When this ajax call returns, i get this 当这个ajax调用返回时,我得到了

Method Executed
Method Executed

How can i make this execute only once? 我怎样才能只执行一次?

您可能使用JSON插件,并且正在调用所有以“ get”开头的方法,以尝试对其进行序列化以进行输出。尝试重命名方法名称

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. 进行两次ajax调用的原因可能是您在代码中两次添加了一个更改侦听器。 Take a look here . 在这里看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM