0

I am using window.onbeforeunload and window.onunload to confirm with user if he really wants to navigate away from a Page. I am calling a Page Method on Server Side to do some database book keeping when user selects 'Leave' option on the dialog using jquery.ajax() method. But my jquery.ajax() to the Page method returns error. It works as expected when calling from other javascript functions. here are the snippets I am using.

In the Page.aspx

 function confirmExit() {
    console.log('In ConfirmExit function unlock='+unlock+'needsave='+needSave);
            timeout = setTimeout(function() {
            }, 1000);
        return "You have pending changes on this request";

        }
        function unloadPage(){
        console.log('In Unload Page function unlock= '+unlock+'needsave= '+needSave);
            if (needSave && unlock) {
                UnlockRequest(null);
                return "You have pending changes on this request";
            }
            clearTimeout(timeout);
        }

Also, I am setting

 window.onbeforeunload = confirmExit;
 window.onunload = unloadPage;

in the Page.aspx. Here's my ajax call to the Server in Unlock Method

dataString = "{'reqID' :'" + selectedVal + "'}";
            var url = "Page.aspx/UnLockRequest";
            console.log('Calling Unlock Request from Unlock Method');
            jQuery.ajax({
                type: 'POST',
                url: url,
                data: dataString,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                processData: 'false',
                success: function () {
                    var r = $("#MainContent_btnSubmit")
                    r.prop('disabled', true);
                    var r = $("#MainContent_releaseLockButton")
                    r.prop('disabled', true);
                    r = $("#MainContent_Button2")
                    r.prop('disabled', true);
                    if ($('#RequestLockedDiv').get(0) !== null)
                        $('#RequestLockedDiv').get(0).setAttribute("style", "display:none;");
                    needSave = unlock = false;
                    $(document).stopIdleTimeoutPlugin();
                    //window.location.reload(false);
                }
            });

The Server Method in Page.aspx.cs

[WebMethod]
    public static void UnLockRequest(string reqID)
    {
        int requestId = 0;
        if (int.TryParse(reqID, out requestId))
        {
            UserInfo lockUser = BLRequest.GetLockerInformationByRequestId(requestId);
            UserInfo currentUser = HttpContext.Current.Session["CurrentUser"] as UserInfo;
            if (lockUser != null && currentUser != null && currentUser.IDSID.ToUpper().Equals(lockUser.ID.ToUpper()))
            {
                BLRequest.UpdateRequestLockStatus(requestId, 0);
            }
        }
    }

I can't see this server request sent neither in Network tab of browser nor in fiddler. BTW I am using Chrome for testing and ASP.NET 4.0. what am I missing.

Programmerzzz
  • 1,237
  • 21
  • 48
  • Show code of servermethod. – Mairaj Ahmad Jul 01 '16 at 03:18
  • `var url = "Page.aspx/UnLockRequest";` should be the url. Also if page name is `Default.aspx` than it should be `Default.aspx/UnLockRequest` – Mairaj Ahmad Jul 01 '16 at 03:37
  • Yeah, they are all set up right, I just changed the names to make up the context more obvious.Yes,URL is `Page.aspx/UnlockRequest` and Page is Page.aspx – Programmerzzz Jul 01 '16 at 03:54
  • Are you entring in the ajax method or not ? – Mairaj Ahmad Jul 01 '16 at 04:34
  • I put couple of console.logs before the Ajax call and they show up fine but this Ajax call I can't track if it's happening as I can't see it in fiddler or network tab in dev tools. But of we have an error call back function with console.logs it shows up as error with status of result. So I am surprised if Ajax call happening at all. Do you see anything missing in my code – Programmerzzz Jul 01 '16 at 05:34
  • Code seems fine to me. But just for a check have a look at this http://stackoverflow.com/questions/27917255/c-sharp-web-method-is-not-calling-in-javascript/27917333#27917333 – Mairaj Ahmad Jul 01 '16 at 06:03
  • Remove `processData: 'false',` it is not needed. – Mairaj Ahmad Jul 01 '16 at 06:04
  • Sure.will remove process data and try. – Programmerzzz Jul 01 '16 at 06:06

0 Answers0