2011-10-24 83 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