2010-11-07 60 views
1

我想调用一个asp.net方法使用jquery/ajax从一个按钮点击模态对话窗口。但我似乎得到的是,并警告说“错误”。我在网上看到了许多类似的例子,但我似乎无法让它工作。我调用的页面/方法(newcall.aspx/savenote)与所有jquery/ajax相同页面。asp.net jquery ajax post

任何任何想法?

感谢,

 var dlg = jQuery("#dialog2").dialog({ 
      bgiframe: false, 
      autoOpen: true, 
      height: 410, 
      width: 800, 
      modal: true, 
      show: 'Transfer', 
      hide: 'Transfer', 
      draggable: true, 
      resizable: true, 
      buttons: { 
       "Cancel": function() { 
        $(this).dialog("close"); 
       }, 
       "Save": function() { 
        var txtnote = document.getElementById("<%=txtNote.ClientID %>").value; 

        $.ajax({ 
         type: "POST", 
         url: "newcall.aspx/savenote", 
         data: txtnote, 
         contentType: "application/json; charset=utf-8", 
         dataType: "json",        
         success: function(msg) { 
          alert(msg); 
         },   
         error: function(XMLHttpRequest, textStatus, errorThrown) { 
          alert(textStatus); 
         } 
        }); 

        $(this).dialog("close"); 
       } 
      } 
     } 

     ); 

背后方法代码:(此刻我只是在 “OK”)

Public Function savenote() As String 
    Return "ok" 
End Function 
+0

缺少WebMethod属性? – tvanfosson 2010-11-07 13:45:48

+0

将alert(textStatus);改为alert(errorThrown);',你会得到什么? – 2010-11-07 13:48:56

+0

现在我得到“undefined” – thegunner 2010-11-07 13:53:12

回答

1

您需要添加WebMethod属性,申报页面的方法Shared,并将您要发送的数据与方法签名进行匹配。这是你应如何改变你的服务器端方法:

<WebMethod()>_ 
Public Shared Function savenote() As String 
    Return "ok" 
End Function 

您可以不改变你的数据参数脱身,但可能不是因为它是无效的JSON。暂时尝试将您的数据参数更改为{}。当您准备开始将txtnote传递到您的服务器端方法时,您需要将其传递为{txtnote: 'Your note string here'}