Mathias
Mathias

Reputation: 233

Call code behind method in jquery, not finding the method

My code behind:

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

My 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)
      }
    });

I always reach the alert where it says "ERROR: " + msg.d.

MyPage.ascx is located in a folder "Controls", so I have tried to set the url: "Controls/MyPage.ascx/accountExists" without any change.

Upvotes: 2

Views: 1967

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34844

ASP.NET AJAX Page Methods are intended to run inside of .aspx pages and not .ascx user controls.

Move your WebMethod logic into an .aspx page and update the AJAX call via jQuery.

Upvotes: 2

Related Questions