2

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.

Mathias
  • 233
  • 3
  • 17
  • Well, what does the error message say? – Amberlamps Aug 02 '13 at 11:50
  • Can't help but noticed you're using an .ascx. You can't call a user control directly, you must include it within an .aspx page. – CodingIntrigue Aug 02 '13 at 11:51
  • @Amberlamps ERROR: False. But it doesn't matter what the input is, even when it should be true it's false. But when I debug this I never reach accountExists except when the page is first loaded. And I'm not running it in Page_Load – Mathias Aug 02 '13 at 11:51
  • Well, I think this kind of methods are called/requested via `PageMethods`, and .ascx is just a control, not like a .aspx. So, you can't request a .ascx file. – mshsayem Aug 02 '13 at 11:54
  • This may help: http://stackoverflow.com/questions/5638184/call-webmethod-in-user-control – mshsayem Aug 02 '13 at 11:58

1 Answers1

2

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.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • I moved accountExists to Default.aspx.cs and changed the url in ajax call to "Default.aspx/accountExists" and now I get "ERROR: undefined". I also tried setting url to "../Default.aspx/accountExists", didn't change anything. – Mathias Aug 02 '13 at 12:00
  • Can you use some browser developer tools (Chrome Developer Tools, Firebug (Firefox) or F12 Tools (IE)) to see if there is an obvious error on your AJAX request to the WebMethod? – Karl Anderson Aug 02 '13 at 13:05
  • Hm, I debugged in VS instead and discovered that even though the "ERROR: undefined" is showing, afterwards it breaks at the "accountExists" function. As in it executes accountExists after the ajax call thinks it failed. – Mathias Aug 02 '13 at 14:26
  • Are either `txtUsername.val()` or `txtPassword.val()` undefined? I don't see either defined in your posted code. – Karl Anderson Aug 02 '13 at 16:33
  • They are yes, they are textboxes and when it breaks at `accountExists` the correct parameters are passed along. – Mathias Aug 03 '13 at 13:15
  • So if you set a break point on the page method, does it correctly have string values for the two parameters? – Karl Anderson Aug 03 '13 at 14:53
  • Yes. The problem as of now is that the ajax call already fires its error function before the break point is hit. – Mathias Aug 03 '13 at 15:13
  • If you hard-code your data to `data: { 'username': 'Mathias', 'password': 'fakepassword'},`, does that work? – Karl Anderson Aug 03 '13 at 15:26
  • It does the same, goes to error function and then breaks at page method. – Mathias Aug 03 '13 at 15:42
  • Where does it break in the page method? Can you see if the parameters (`username` and `password`) have values in the page method or not? – Karl Anderson Aug 03 '13 at 19:42
  • It breaks where I set the break point, which is on the first row of the function. `username` and `password` are passed correctly to the page method. – Mathias Aug 03 '13 at 20:44
  • So what is the rest of the code, you only posted the method definition with no body? – Karl Anderson Aug 03 '13 at 20:54
  • I figured that could be an issue as well so I tried to just return a simple string, but it again did no affect to solve the issue. – Mathias Aug 03 '13 at 21:02
  • In order to get the desired functionality I did a workaround to avoid an ajax call. But your answer is correct in any case, there was just something else in the way for me. – Mathias Aug 05 '13 at 13:44
  • Great, glad you figured it out. Good luck to you. – Karl Anderson Aug 05 '13 at 13:55