2011-10-24 81 views
0

我想發佈請求到服務器,但它不會打到我使用調試器時?ajax請求Mootools到asp.net webforms

服務器:

public partial class _Default : System.Web.UI.Page 
    { 


     public string HitThis() 
     { 
      return "braza"; 

     } 
    } 


<script type="text/javascript"> 

     var myRequest = new Request({ 
      method: 'post', 
      url: '/Default.aspx/HitThis', 
      onSuccess: function() { 
       alert('good'); 
      }, 
      onFailure: function() { 
       alert('nope'); 
      } 


     }); 

     myRequest.send(); 


</script> 

回答

0

如果你希望能夠打電話給你HitThis方法,你需要做的是方法,靜態的,與Web Method屬性裝飾它,使你的ScriptManager

頁面方法

例子:

<asp:ScriptManager ID="ScriptManager" runat="server" 
     EnablePageMethods="true" /> 


    [WebMethod] 
    public static string HitThis() 
    { 
     return "Hello World"; 
    } 
+0

scriptmanager?我正在使用mootools? – user603007

+0

是的,但你使用的是asp.net,不是嗎?添加我發佈到您的頁面的標記。它不會傷害你,它可以讓你在服務器端調用你的'HitThis'方法。 – Icarus

0

您需要先了解ASP.NET AJAX腳本服務或PageMethod的WOR KS!頁面方法必須用WebMethod屬性進行修飾,並且需要是靜態的。

[WebMethod] 
public static string HitThis() 
{ 
} 

請參閱this article,它說明了使用jquery的調用頁面方法。你可以採用它與mootools。但是,請注意,頁面方法需要內容類型爲JSON數據,響應也將使用JSON。

也許你可以使用Request.PathInfo在ASP.NET頁面中編寫自己的佈線邏輯,如果你想使用普通表單發佈。例如,

protected void Page_Load(object sender, EventArgs e) 
{ 

    if (this.Request.PathInfo == "HitThis") 
    { 
     HitThis(); 
    } 
} 

在你的方法,你需要使用的Response(HttpResponse對象)和修改響應後,您需要結束它(HttpResponse.End),使正常的頁面處理不會發生。如果你的方法需要參數,那麼你必須通過表單數據和/或查詢字符串來傳遞它們。

+0

我想:var jSonRequest = new Request.JSON({url:'default.aspx/HelloWorld'})。send();但是這不起作用:( – user603007