在jquery中调用方法后面的代码,而不是找到方法

时间:2013-08-02 11:48:21

标签: c# javascript jquery asp.net web-services

我的代码背后:

[WebMethod]
public bool accountExists(string username, string password) {
//code...
}

我的jquery:

$.ajax({
      type: "POST",
      url: "MyPage.ascx/accountExists",
      data: JSON.stringify({ username: txtUsername.val(), password: txtPassword.val()}),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        alert(msg.d)
      },
      error: function(msg) {
        alert("ERROR: " + msg.d)
      }
    });

我始终会在"ERROR: " + msg.d显示警报。

MyPage.ascx位于“控件”文件夹中,因此我尝试设置url: "Controls/MyPage.ascx/accountExists"而不做任何更改。

1 个答案:

答案 0 :(得分:2)

ASP.NET AJAX页面方法旨在在.aspx页面内运行而不是.ascx用户控件。

将您的WebMethod逻辑移至.aspx页面并通过jQuery更新AJAX调用。