locked
How to call Server method from JavaScript. RRS feed

  • Question

  • User-886811842 posted

    Hi,

       Is there any way to invoke server function from javascirpt method ?
    eg: i need to call GetTime() from javascript and assign value to var. 

    Function

    Wednesday, October 22, 2014 9:25 AM

Answers

  • User281315223 posted

    The only way that you would be able to do this would be through AJAX Requests, assuming your are referring to the actual Session (that you can store values in) within ASP.NET as Javascript alone cannot access it (client-side vs server-side).

    I suppose you could define two WebMethods within your ASPX page and then make AJAX Requests to either set or retrieve the value from the Session :

    [WebMethod]
    public static string SetTimeInSession(string time)
    {
         Session["CurrentUserBiziviztoke"] = time;
    }
    
    [WebMethod]
    public static string RetrieveTimeFromSession()
    {
         return Session["CurrentUserBiziviztoke"];
    }

    This way if you needed to set your Session key through AJAX, you could use the following code in your page :

    <!-- Example jQuery Script Reference -->
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <!-- Your Script -->
    <script type='text/javascript'>
        // When your page loads
        $(function(){
              $.ajax({
                type: "POST",
                url: "YourPage.aspx/SetTimeInSession",
                data: '{ time: "' + new Date().getTime().toString() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                      // Your Time was set through Javascript
                }
              });
        });
    </script>

    and you could retrieve it essentially the same way :

    $.ajax({
                type: "POST",
                url: "BusDetails.aspx/RetrieveTimeFromSession",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                      alert("The time from the server is " + data.d);
                }
    });

    Could you elaborate a bit more on exactly what you are trying to do and we might be able to provide a more detailed response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2014 9:51 AM

All replies

  • User281315223 posted

    The only way that you would be able to do this would be through AJAX Requests, assuming your are referring to the actual Session (that you can store values in) within ASP.NET as Javascript alone cannot access it (client-side vs server-side).

    I suppose you could define two WebMethods within your ASPX page and then make AJAX Requests to either set or retrieve the value from the Session :

    [WebMethod]
    public static string SetTimeInSession(string time)
    {
         Session["CurrentUserBiziviztoke"] = time;
    }
    
    [WebMethod]
    public static string RetrieveTimeFromSession()
    {
         return Session["CurrentUserBiziviztoke"];
    }

    This way if you needed to set your Session key through AJAX, you could use the following code in your page :

    <!-- Example jQuery Script Reference -->
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <!-- Your Script -->
    <script type='text/javascript'>
        // When your page loads
        $(function(){
              $.ajax({
                type: "POST",
                url: "YourPage.aspx/SetTimeInSession",
                data: '{ time: "' + new Date().getTime().toString() + '" }',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                      // Your Time was set through Javascript
                }
              });
        });
    </script>

    and you could retrieve it essentially the same way :

    $.ajax({
                type: "POST",
                url: "BusDetails.aspx/RetrieveTimeFromSession",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data) {
                      alert("The time from the server is " + data.d);
                }
    });

    Could you elaborate a bit more on exactly what you are trying to do and we might be able to provide a more detailed response.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2014 9:51 AM
  • User-886811842 posted

    HI Thanks for your reply....surly i will try this on my code.
    but i am using this method on share point web part ascx page its has some limitation for using ajax method.

    Wednesday, October 22, 2014 10:12 AM
  • User281315223 posted

    I can't personally speak to the limitations of using this within the context of a Sharepoint environment as I honestly don't have much experience there. I would recommend giving it a try (as you are already using jQuery so it might still work) and if not, we can explore a few other options depending on exactly what you are trying to accomplish.

    Wednesday, October 22, 2014 10:16 AM
  • User1918509225 posted

    Hi sujikumar,

    For your issue is related with ajax in sharepoint development,i suggest that you can post your issue to the link below for a professional solution:

    https://social.msdn.microsoft.com/Forums/sharepoint/en-US/home?forum=sharepointdevelopment

    Best Regards,

    Kevin Shen.

    Wednesday, October 22, 2014 10:23 PM
  • User-886811842 posted

    Yea exactly its worked for me in both dev env. thank you so much.. 

    Thursday, October 23, 2014 1:20 AM