Ajax using JQuery in ASP NET c

0 votes

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.

Jun 15 in JQuery by gaurav
• 12,980 points
8 views

1 answer to this question.

0 votes

jQuery Ajax in ASP.Net

  1. $.ajax({  
  2.     type: "POST",  
  3.     url: "CS.aspx/MethodName",  
  4.     data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',  
  5.     contentType: "application/json; charset=utf-8",  
  6.     dataType: "json",  
  7.     success: OnSuccess,  
  8.     failure: function(response) {  
  9.         alert(response.d);  
  10.     }  
  11. });
answered Jun 15 by rajatha
• 4,060 points

Related Questions In JQuery

0 votes
1 answer

Download pdf file using jquery ajax

Download PDF file with button click using ...READ MORE

answered May 30 in JQuery by gaurav
• 12,980 points
157 views
0 votes
0 answers

using for loop in jquery

$(document).ready(function() { $for(i=1;i<8;i++) ...READ MORE

Jun 6 in JQuery by Edureka
• 13,640 points
7 views
0 votes
1 answer

Show Page Loading Spinner on Ajax Call in jQuery Mobile

 Use the ajaxStart() and ajaxStop() Method While working ...READ MORE

answered Jun 10 in JQuery by gaurav
• 12,980 points
17 views
0 votes
1 answer

How to import jQuery UI using ES6/ES7 syntax?

Hii, Add a alias in webpack config: resolve: { ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
5,020 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
247 views
0 votes
1 answer

Writing SEO friendly url gets infinite loop asp.net

you should know that regex avoid cases ...READ MORE

answered Feb 11 in Others by narikkadan
• 10,840 points
136 views
0 votes
1 answer

jQuery UI " $("#datepicker").datepicker is not a function"

I struggled with a similar problem for ...READ MORE

answered Feb 17 in Java by Soham
• 8,730 points
895 views
0 votes
0 answers

How to get Bitcoin value for corresponding USD value in ASP.NET C#?

I want to get the Bitcoin value ...READ MORE

Mar 1 in Blockchain by Soham
• 8,730 points
93 views
0 votes
1 answer

Abort Ajax requests using jQuery

Instead of aborting, you can choose to ...READ MORE

answered Jun 16 in JQuery by rajatha
• 4,060 points
15 views
0 votes
1 answer

what are the advantages and disadvantages of making ajax calls using jquery?

Advantages of AJAX Reduce server traffic and increase ...READ MORE

answered Jun 21 in JQuery by rajatha
• 4,060 points
8 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP