I have a legacy WebService referenced in a ScriptManager
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/AJAX_SERVICE.asmx" />
</Services>
</asp:ScriptManager>
And defined like
<System.Web.Script.Services.ScriptService()> _
Public Class CAjaxService : Inherits System.Web.Services.WebService
<WebMethod(enableSession:=True)> _
Public Sub SomeMethod()
...
End Sub
...
End Class
After that to call the method from JavaScript client code I can simple do
CAjaxService.SomeMethod()
I need to execute this call when user navigates away from the current page, so I placed it into page unload event:
function pageUnload(sender, args) {
CAjaxService.SomeMethod()
}
The problem is - the call is async and page navigates away before call is complete (if I place alert()
after the call - it goes thru). I've seen similar problems with jQuery ajax calls (and recommended solution to make a synchronous call) but I am not sure if this is possible in MS Ajax.
What would be the way to execute (and complete) an AJAX call upon user navigating away from current page?
I ended up implementing my own function
where I do a manual synchronous post request. Not the most elegant method, but it works.