Asked by:
How to cancel the call to web service

Question
-
User798666889 posted
I am trying to write an online dictionary using ajax. I use a web service method to find the word user entered. but if the user click the "search" button more than one time or try to enter another word within the time browser is waiting for the response from the server, how can I cancel the previous request to call the web service method using js? [:S]
Tuesday, December 5, 2006 2:55 AM
All replies
-
User1112706252 posted
Have a look on this thread http://forums.asp.net/thread/1462384.aspx
Hope it helps
Wednesday, December 13, 2006 10:03 AM -
User1955380210 posted
Hi Lenyado,
I have encountered this problem recently and here is my solution.
- in js, I have a global variable call request.
- when calling the web service from javascript, don't use the wrapped javascript functions that the AJAX ASP.NET generated.
- use request = Sys.Net.WebServiceProxy.invoke(servicePath, methodName, useGet, params, onSuccess, onFailure, userContext, timeout()); instead.
- then create a dummy onFailed
- To cancel the request, use request.get_executor().abort();
Tuesday, January 16, 2007 12:02 PM -
User1112706252 posted
Here is another solution(just saw your one which is cool too):
- add a kind of OnStart and OnEnd event handlers to the web service call you want, just before making the actual web service call, using
Sys.Net.WebRequestManager.add_invokingRequest(CallsStarted);
and
Sys.Net.WebRequestManager.add_completedRequest(CallsEnded);
- CallsStarted() and CallsEnded() event handlers have 2 parameters 'sender' and 'events'.
- you can use the sender to call .abort() on it
This is epsecially usefull when you have multiple web service calls and you want to cacel specific web service call requests.
Cheers.
Adi
Tuesday, January 16, 2007 12:26 PM -
User1955380210 posted
Hi all,
After looking at the script more carefully, I see that there is a simpler way to retrieve the WebRequest object of the current request. The javascript functions provided for us do not return the request object. If we want to retrieve the request object, we need to call the functions through the _staticInstance. For example, I have WebService1.Method1. To retrieve the request object, instead of using WebService1.Method1, I use WebService1._staticInstance.Method1 to call the service.
Wednesday, January 17, 2007 10:07 AM -
User-285174071 posted
When I call this ( request.get_executor().abort(); ) nothing aborts. I do not get a JS error, so I know the function exists, but the data keeps streaming from the web service. My control continues to load data even after the abort is called. Any thoughts?Monday, January 22, 2007 2:27 PM -
User1013556049 posted
Hi,
I'm also trying to abort a web service and I'm not sure how to do it. I'm using ASP.NET AJAX RC. I have a page with 2 buttons. The first one is for calling the web service and the second one is for aborting the call. Here is the code:
<
script language="javascript" type="text/javascript">
var abortExecutor;
function CallService()
{
Sys.Net.WebRequestManager.add_invokingRequest(On_InvokingRequest);
retVal = SimpleService.HelloWorld(document.getElementById('Text1').value, OnComplete, OnError);
Sys.Net.WebRequestManager.remove_invokingRequest(On_InvokingRequest);
}function
On_InvokingRequest(executor, eventArgs)
{
abortExecutor = executor.get_defaultExecutorType();
}function
Button1_onclick()
{
CallService();
}function
OnComplete(result)
{
alert(result);
}function
OnError(result)
{
alert("Error: " + result.get_message());
}function
Button2_onclick() {
abortExecutor.abort();
alert("aborted");
}Unfortunatelly I get this: "Microsoft JScript runtime error: Object doesn't support this property or method" at the abortExecutor.abort(). I've also tried with the following:
function On_InvokingRequest(executor, eventArgs)
{
abortExecutor = executor;
}but I get the same error.
What am I doing wrong?
Thanks,
DIB.
Wednesday, January 24, 2007 9:41 AM -
User264985426 posted
The defaultExecutorType will return a string which is the type of the default executor What you need is the actual executor which can be used to abort the request. To get this you need to modify your code as following:
function On_InvokingRequest(executor, eventArgs) { abortExecutor = eventArgs.get_webRequest().get_executor(); }
Wednesday, January 24, 2007 10:44 AM -
User1013556049 posted
It seems that I've found the answer:
function
On_InvokingRequest(executor, eventArgs)
{
var currentRequest = eventArgs.get_webRequest();
abortExecutor = currentRequest.get_executor();
}And it works!!!
Wednesday, January 24, 2007 10:45 AM -
User1013556049 posted
It seems we've wrote in the same time... :)
Thanks, anyway!Wednesday, January 24, 2007 10:51 AM -
User798666889 posted
many thanks, guys!Saturday, February 3, 2007 12:40 PM -
User1603929102 posted
Hi guys, sorry to resurrect this thread but even though I call the abort() method of the executor and the error handler fires, the web service call still comes back after a while with the data and fires my Success event. Any ideas why?Wednesday, December 5, 2007 4:38 PM