Ajax / Jquery calling web method / service every time intervals

I have an ASP.Net web application with jquery implemented on the client side. The client side jquery script makes an asynchronous call to a web method in the server side code. The call returns an open status record (active / inactive) that jquery uses to update the user. The goal is for jquery to re-invoke the server as soon as an open record is inactive, then we need to display a message to the user so that you are no longer associated with that record. I installed TimeInterval in one of HiddenFieldValues and go to JQuery / ajax function.

This function is written in a separate JavaScript file and listed in my ASPX script Manager page. I need to pass the "interval" from the server side, which is configured in the .config file.

function myWebServiceFunction(val1, val2, val3, interval) {
                $.ajax({
                    type: "POST",
                    url: "/Application/WebServices/MyService.asmx/CheckFunction",
                    data: "{'val1':'" + val1 + "','val2':'" + val2 + "','val3':'" + val3 + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    success: function (msg) {
                        debugger;
                        var obj = function callbackfunction() {
                            myWebServiceFunction(HealthCarrierShortName, authID, networkID, interval)
                        }
                        if (!msg.d) {


                            window.setTimeout(obj, interval);
                        }
                        else {
                            // Still need to implement how to display to user if the record is not long associated to that user. help me in this too
                        }
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('AJAX failure');
                    }
                });
            }

      

On my server side I was using RegisterStartUpScript at the end of the Page_load method and calling this JQuery function

        ScriptManager.RegisterStartupScript(Me.Page, Page.GetType(), "AuthLockPolling", " myWebServiceFunction('" + val1HiddenField.Value + "','" + val2HiddenField.Value + "','" + val3HiddenField.value+ "','" + val4HiddenField.value+  ");", True)

      

But it is not working correctly (not sure why exactly). My JQuery function is not being called at all. I am testing by putting a debugger in my script and it didn't hit.

You still need to implement a way to display the message to the user if that entry is not associated with that user, like in an alert / popup. Please help me with this part too.

Please advise me what went wrong and how to fix it. Thanks in advance!

+3


source to share


1 answer


How to solve it:

RegisterStartupScript

can be confusing (ask me how I know!). Are you using this on a page that uses partial page updates (i.e. has controls UpdatePanel

)? If not, you should use the method in the class ClientScriptManager

(instead of ScriptManager

).

If it is used on a page with a partial page refresh for a control that is inside an UpdatePanel, the first parameter must be control in UpdatePanel

, not Page

.



And a hint for debugging: test it by running through JavaScript code that fails, eg alert('Hello, World!');

. This can help you determine if the problem is in the call to RegisterStartupScript or in your myWebServiceFunction.

Finally, here's the Microsoft documentation: https://msdn.microsoft.com/en-us/library/bb310408(v=vs.110).aspx . Since there are methods with the same name in different classes, please read the documentation carefully, correct, carefully.

+1


source







All Articles