Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Response Headers from ASP.Net PageMethod Call

When using ASP.Net Ajax to call PageMethods, how can I access the Http response headers from the "success" method?

For example:

PageMethods.DoSomething(
   function(result){successMethod(result)},
   function(error){errorMethod(error)}
);

function successMethod(result){
    //------how can I access the Http response headers from here? ------
}

Thanks for any help

like image 680
James Avatar asked Jan 08 '10 18:01

James


1 Answers

In your example, PageMethods.DoSomething should have a return value equal to WebRequest if it's an asp.net web service proxy. This is provided so that you can manipulate the request after you've initiated it (i.e. cancel it etc).

With this class you have an add_completed method which you can use to add a handler for when the web request completes. The signature for the callback is function OnWebRequestCompleted(executor, eventArgs), and the executor parameter in this enables you to get hold of extra response information. For example, you can get hold of the response headers with executor.getAllResponseHeaders(); which should be a map (named collection) of header names and values.

So if you add a handler to the web request's completed event immediately after making the service method call, it should work (there's no web service in the world that can respond faster than two consecutive lines of code!).

The previous hyperlink to WebRequest contains a full example of how wire this up. Notice, however, that this code uses the WebRequest directly.

Asp.Net Ajax Web Service proxy classes use the WebServiceProxy class, and each proxy method ultimately call its invoke method, which returns the WebRequest instance.

like image 97
Andras Zoltan Avatar answered Nov 12 '22 18:11

Andras Zoltan