killer
killer

Reputation: 602

Not able to call a webmethod in a code page from master page

I have a webmethod in c# asp.net and a jquery code to call that method. If I place code in webService page like myservice .asmx it works fine. I call it from the same master page. But when I call it from .aspx page It doesnt work at all. I am calling this webservice on button click event. Button click is fired in both situations I checked it through alert in javascript.

MY Master page code for calling webmethod is as:

 <%--  WebService Data--%>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
     <script type ="text/javascript" >
         $(document).ready(function () {
             $("#btni").click(function () {
                 var dataSent = "{roll:" + 1 + ",name:\"" + "Mubashir\"}"                               
                 $.ajax({    
                     type: "Post",
                     url: "EmailList.aspx/heelo",
                     data: dataSent,
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     success: function (response) {
                         var studentInfo = response.d;
                         alert(JSON.stringify(response.d));
                         $("#output").empty();
                         $.each(studentInfo, function (index, Info) {
                             //alert(index);
                             //alert(index.Info);
                             $("#output").append(
                                 '<strong>Roll : </strong>' + Info.roll + ' ' +
                                  '<strong>Name : </strong>' + Info.name + ' ' +
                                   '<strong>Address : </strong>' + Info.address + '<br/> '
                                 );    
                         });  
                     },    
                     failure: function (msg) {
                         alert(msg.d);    
                         $("#output").text(msg);    
                     }   
                 });   
             });   
         });
    </script>

<%-- WebService Data--%>
webmethod on EmailList.aspx.cs page is like this:

[WebMethod]
public string heelo(int roll, string name)
{

    return "Mubashir";
}
//WebService

If I place same method on myservice.asmx code file it works fine.

Upvotes: 3

Views: 1213

Answers (1)

صفي
صفي

Reputation: 1068

Make your method static

[WebMethod]
public static string heelo(int roll, string name)
{

    return "Mubashir";
}

To know more about Why do ASP.Net AJAX PageMethods have to be static read this.

Upvotes: 2

Related Questions