0

Im trying to call a c# method on select changed event of a dropdown list,The select change event triggers but the ajax does not work

     <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

[WebMethod]
     public static void getdata(String text)
        {
            //do stuff
        }
1
  • 1
    It's error: function(...) not Error: function(...) and alert('error') isn't that helpful either. Check the docs and use the supplied parameters of the error handler Commented Aug 21, 2013 at 7:10

3 Answers 3

2

You have to decorate getdata method with [WebMethod] attribute. In your c# code [WebMethod] is missing.

Sign up to request clarification or add additional context in comments.

Comments

1

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');
                      }
                  });

Comments

0

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

1 Comment

sorry forgot to add that in question.I had used webmethod

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.