开发者

How to avoid calling a ajax method more than once?

开发者 https://www.devze.com 2022-12-16 13:56 出处:网络
I have a .NET application that access methods located on handlers through AJAX calls. This AJAX calls are fired, generally, when the user cl开发者_Go百科icks on certain buttons. My problem happens whe

I have a .NET application that access methods located on handlers through AJAX calls. This AJAX calls are fired, generally, when the user cl开发者_Go百科icks on certain buttons. My problem happens when the user clicks the buttons more than once. It messes up with all my object´s state.

How do u take care of it? I know I can block the user click until the first call finishes, but I would like to find a solution to take care of this matter in the server side. Any idea?

Thanks!


I believe that this is best achieved using client code. If you are using jQuery then one() may be an elegant solution. Alternatively you could set a variable to true when starting a request and set it to false when finished. Then prevent all further requests while the variable is set to true.

Doing it server side won't work. The server may have finished processing, but the message hasn't arrived to the client yet. The client then makes a new request. The server reacts to it as it is a new one. Havoc may occur in cases like this.


in the onclick method, remove the button's onclick handler, then run the ajax call in the background with setTimer, and return true...

when the ajax completes, you can optionally re-attach the onclick handler.


Use jquery blockUI plugin. when user click on button first time then lock user page. after completing user task unblock the page.

Refer http://malsup.com/jquery/block/#

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号