Unable to call web method on code page from master page

I have a webmethod in C # asp.net and jquery code to call this method. If I post the code on a webService page like myservice.asmx it works fine. I call it from the same homepage. But when I call it from the .aspx page, it doesn't work at all. I am calling this web service on a button click event. A click on a button is triggered in both situations when I checked it with an alert in javascript.

MY Master code to call web method:

 <%--  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 in EmailList.aspx.cs page looks like this:

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

    return "Mubashir";
}
//WebService

      

If I place the same method in myservice.asmx file it works fine.

+3


source to share


1 answer


Make your method static

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

    return "Mubashir";
}

      



More details on why ASP.NET AJAX PageMethods should be static

read this .

+2


source







All Articles