Enigmatic Mind Enigmatic Mind - 7 months ago 48
Javascript Question

Ajax using JQuery in ASP .NET c#

I am trying to use JQuery ajax in asp .net c#. The code I am using is ...

HTML FORM:

<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
onclick = "ShowCurrentTime()" />
</div>


JQuery Part :



<script type="text/javascript">
function ShowCurrentTime() {
//alert(window.location.pathname);
$.ajax({
type: "POST",
url: "Display.aspx/GetCurrentTime",
data: '{name: "' + $("#< %=txtUserName.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>


CS PArt:

using System.Web.Services;

This part below is under partial class which inherits from Page class.

[WebMethod]

public static string GetCurrentTime(string name)
{

return "Hello "+ name +"! " + Environment.NewLine + "The Current Time is: "
+ DateTime.Now.ToString();
}


Problem:
It is not working. Please let me know if I am missing any setting or any name space. Or any thing. URL I Put in ajax Call is correct as I verified it by
var pathname = window.location.pathname; I supplied data also while calling ajax.

Answer

Maybe changing

 failure: function (response) {
        alert(response.d);
    }

to

 error: function (response) {
        alert(response.d);
    }

Will help find the issue. I don't see a failure callback in the docs - http://api.jquery.com/jquery.ajax/