简体   繁体   中英

How to kill a javascript async call to a .net webservice?

My javascript code is calling a asp.net webservice, so i have a call to the webservice something like this:

MyWebservice.GetData(param, ResponseReceived, ResponseTimeOut, ResponseError);

When the webservice returns data, ResponseReceived method is called.

However sometimes the user might navigate to another url before the webservice call actually returns, in such a scenario FireFox throws an Error saying 'An error occured oricessubg the request. The server method GetData failed'

So my question is how can i kill the async call when the user navigates to another page or makes another request to the webservice? I know in a normal XMLHttpRequest i could have called Abort method, but not sure how to make it work with the above webservice proxy.

A good practice would be to keep your common functions in a common place, which is accessible from all of your pages.

This would include the OnError function. That way you can safely reference that same function from all of your pages. If you need to provide custom Error Functionality, you can either override this function on your page, or include a handler in your common function, and call if it was assigned.

A good place to put such common function would be inside a root master page, or a shared JavaScript file referenced from root master page.

What is good about this, is that hopefully your OnError function does some logging, so you can get an idea of what fails and how often so that you can design your app accordingly.

Ok I figured out a solution, I can call get_executor() method to get an instance of the XmlHttpExecutor class, on which I can call the abort method. Hope it helps others facing a similar problem.

I'm still eager to find out other solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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