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, 2022 in JQuery by gaurav
• 23,260 points
4,039 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, 2022 by rajatha
• 7,680 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, 2022 in JQuery by gaurav
• 23,260 points
20,910 views
0 votes
0 answers

using for loop in jquery

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

Jun 6, 2022 in JQuery by Edureka
• 13,690 points
576 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, 2022 in JQuery by gaurav
• 23,260 points
1,434 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,840 points
7,965 views
0 votes
0 answers

UL subdrop not working with jquery in Asp.Net C#

Keep searching why sub drop not working ...READ MORE

Jul 29, 2022 in Web Development by gaurav
• 23,260 points
384 views
0 votes
0 answers

How to upload Iform File in iframe ( was binding null) (javascript/jquery)

In asp.net core , i define ProductUpdateRequest has  public ...READ MORE

Aug 19, 2022 in Web Development by gaurav
• 23,260 points
1,588 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
911 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, 2022 in Others by narikkadan
• 63,600 points
803 views
0 votes
1 answer

Abort Ajax requests using jQuery

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

answered Jun 16, 2022 in JQuery by rajatha
• 7,680 points
762 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, 2022 in JQuery by rajatha
• 7,680 points
1,862 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP