简体   繁体   English

无法执行对C#方法的Ajax调用

[英]Unable to execute Ajax call to c# method

Im trying to call ac# method on select changed event of a dropdown list,The select change event triggers but the ajax does not work 我试图在下拉列表的选择更改事件上调用ac#方法,选择更改事件触发但ajax不起作用

     <script type="text/javascript">
          $(document).ready(function () {


              $('body').delegate('#drpselect1', 'change', function () {
                  var groupname = $("#drpselect1 option:selected").text();
                  alert(groupname);
                  $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: "sample.aspx/getdata",
                      dataType: "json",
                      {"text":groupname},
                      success: function () {
                         alert("works");
                          // window.location.href = "ClubCreation.aspx";
                      },
                      Error: function () {
                          alert('error');
                      }
                  });
             /*     $.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: "sample.aspx/getdata",
                        data:{"text":groupname}
                                          dataType: "json",
                      success: function () {
                          alert('Successfully Saved');
                          //window.location.href = "ClubCreation.aspx";
                      },
                      Error: function () {
                      }


    });*/

          });


      });




</script>

c# method C#方法

[WebMethod]
     public static void getdata(String text)
        {
            //do stuff
        }

You have to decorate getdata method with [WebMethod] attribute. 您必须使用[WebMethod]属性装饰getdata方法。 In your c# code [WebMethod] is missing. 在您的C#代码中[WebMethod]丢失。

try this 尝试这个

check this line 检查这行

                      data:'{"text":"'+groupname+'"}',//put "data:"

now, 现在,

$.ajax({
                      type: "POST",
                      contentType: "application/json; charset=utf-8",
                      url: "sample.aspx/getdata",
                      dataType: "json",
                      data:'{"text":"'+groupname+'"}',//put "data:"
                      success: function () {
                         alert("works");
                          // window.location.href = "ClubCreation.aspx";
                      },
                      Error: function () {
                          alert('error');
                      }
                  });

Probabky you missing attributes: 可能会丢失属性:

[System.Web.Services.WebMethod()] 
public static void getdata(String text)

Look here for more informations: Using jQuery to directly call ASP.NET AJAX page methods 在这里查看更多信息: 使用jQuery直接调用ASP.NET AJAX页面方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM