2013-08-21 78 views
0

我试着打电话给在下拉列表中选择更改事件交流#方法,选择变化事件触发,但阿贾克斯不工作无法执行Ajax调用C#方法

 <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#方法

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

它的'错误:函数(...)'不'错误:函数(...)'和警报('错误')'也没有那么有用。检查[文档](http://api.jquery.com/jquery.ajax)并使用错误处理程序 – Andreas

回答

1

试试这个

检查此行

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

$.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'); 
         } 
        }); 
+0

类似于魅力,谢谢 – techno

2

您必须用 [WebMethod]属性来修饰getdata方法。 在您的C#代码[WebMethod]丢失。

+0

提供的参数,请参阅编辑 – techno