无法从母版页调用代码页中的 Web 方法

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

本文关键字:Web 方法 代码 母版页 调用      更新时间:2023-09-26

我有一个 c# asp.net 中的 webmethod 和一个 jquery 代码来调用该方法。如果我将代码放在像myservice .asmx这样的webService页面中,它可以正常工作。我从同一个母版页调用它。但是当我从页面调用它时.aspx它根本不起作用。我正在按钮单击事件上调用此网络服务。在两种情况下都会触发按钮单击,我通过 javascript 中的警报进行了检查。

用于调用 webmethod 的 MY 母版页代码为:

 <%--  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>

<%-- Web 服务数据 - %>
电子邮件列表页面上的webmethod.aspx.cs如下所示:

[WebMethod]
public string heelo(int roll, string name)
{
    return "Mubashir";
}
//WebService

如果我在myservice.asmx代码文件上放置相同的方法,它可以正常工作。

让你的方法static

[WebMethod]
public static string heelo(int roll, string name)
{
    return "Mubashir";
}

要了解有关为什么必须 ASP.Net AJAX PageMethods的更多信息static请阅读本文。