Enigmatic Mind Enigmatic Mind - 1 year ago 148
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 ...


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

JQuery Part :

<script type="text/javascript">
function ShowCurrentTime() {
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) {
function OnSuccess(response) {

CS PArt:

using System.Web.Services;

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


public static string GetCurrentTime(string name)

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

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 Source

Maybe changing

 failure: function (response) {


 error: function (response) {

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