【问题标题】:Not able to call a webmethod in a code page from master page无法从母版页调用代码页中的 Web 方法
【发布时间】:2015-01-08 10:01:33
【问题描述】:

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

调用webmethod的我的主页代码如下:

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


EmailList.aspx.cs页面上的webmethod是这样的:

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

    return "Mubashir";
}
//WebService

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

【问题讨论】:

    标签: javascript c# jquery asp.net web-services


    【解决方案1】:

    让你的方法static

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

    要了解更多关于为什么 ASP.Net AJAX PageMethods 必须是static,请阅读this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2012-01-24
      相关资源
      最近更新 更多