简体   繁体   English

jQuery Ajax错误导致无限循环

[英]Jquery ajax error causing infinite loop

When a user tabs into a text box on a page it fires off the onfocus event to call a web service via jquery AJAX call. 当用户在页面上的文本框中切换时,它会触发onfocus事件以通过jquery AJAX调用来调用Web服务。

If there is an error with the ajax call, I want to display a message to the user via an alert box but it is causing an infinite loop. 如果ajax调用出现错误,我想通过警报框向用户显示一条消息,但这会导致无限循环。 It will display the alert box, the user clicks okay and the alert box reappears. 它将显示警报框,用户单击“确定”,然后再次出现警报框。

How do I resolve this issue? 我该如何解决这个问题?

function DoSomething() {
    var url = baseUrl + "/MyMethod";

    $.ajax({
        url: url,
        data: { input: 'MyData' },
        error: function(e) {
            alert("Display message");
        }
    });
}

<asp:Textbox id="txtMyTextBox" runat="server" onfocus="DoSomething" />

Bind to an actual user action like onclick or onchange , instead of onfocus . 绑定到诸如onclickonchange类的实际用户操作,而不是onfocus If the user hasn't entered anything, there's probably nothing useful to send to the server, right? 如果用户没有输入任何内容,则可能没有任何有用的信息发送到服务器,对吗?

<asp:Textbox id="txtMyTextBox" runat="server" onclick="DoSomething" />

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

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