Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET
Tip/Trick

Ajax Call from Master Page to webservice (asmx) through JQuery

Rate me:
Please Sign up or sign in to vote.
4.08/5 (8 votes)
5 Nov 2013CPOL 27.5K   3   3
This tip describes the steps to access server side web service (asmx) from master page JavaScript.

Introduction

This tip describes the steps to access server side web service (asmx) from master page JavaScript. This can be helpful in situations when you want to prevent full page postback and need to communicate from client side to server side from master page.

Using the Code

You need to follow the steps given below:

  1. Add webservice (asmx file) that contains the server side function you want to access from jquery:
    C#
    [WebMethod]
        public string HelloWorld(int Variable1, string Variable2)
        {
            return "Hello World";
        }   
  2. Uncomment the line to allow access to webservice from JavaScript:
    C#
    [System.Web.Script.Services.ScriptService]    
  3. Add master page and add the markup after the form tag:
    ASP.NET
    <span id="ArticleContent">
    <h1>Access server side from jquery ajax from master page</h1>
    <label title="Click to access server from javascript" 
    onclick="SetTabSessionValue(12, 'Hello');">
    Click to access server from javascript</label>
    
    </span> 
  4. Add the JavaScript function that is called when you click on the label to master page. Adjust your webservice path and name accordingly (my webservice name is WSAccessMPJquery.asmx). Also adjust the path for jquery min.js.
    JavaScript
    <script src="js/jquery-1.8.3.min.js" 
    type="text/javascript"></script>
    <script>
    function SetTabSessionValue(Variable1, Variable2) {
    
            var param = "{'Variable1':" + JSON.stringify(Variable1) + 
            ",'Variable2':" + JSON.stringify(Variable2) + "}";
    
            $.ajax({
                type: "POST",
                url: "WSAccessMPJquery.asmx/HelloWorld",
                data: param,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: SetTabSessionValueSucceed,
                error: SetTabSessionValueFailed
            });
        }
        function SetTabSessionValueSucceed(result) {
            alert("text from server: "+ result.d);
        }
    
        function SetTabSessionValueFailed() {
            alert('call failed');
        }
    </script>
  5. Add normal aspx page that uses master page and run the solution.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelancer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe webservice method is not executing for me Pin
yedhuvamshi9-Mar-15 20:34
yedhuvamshi9-Mar-15 20:34 
AnswerRe: The webservice method is not executing for me Pin
Muhammad Waqas Iqbal11-Mar-15 18:48
Muhammad Waqas Iqbal11-Mar-15 18:48 
GeneralRe: The webservice method is not executing for me Pin
yedhuvamshi11-Mar-15 19:09
yedhuvamshi11-Mar-15 19:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.