2013-08-21 77 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