2

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.

10
  • Do you have jquery.unobtrusive-ajax.min.js Commented Jan 25, 2016 at 17:19
  • Does anything go out back to the server. Load the tool Fiddler to ensure you can see a request going out. Narrow it down to client or server so we can help troubleshoot. Commented Jan 25, 2016 at 17:23
  • 1
    what does not work ? could we see your console ? Commented Jan 25, 2016 at 17:23
  • @Stralos I can see the console. There is no call from any where. Commented Jan 25, 2016 at 17:25
  • @HerGiz I think jQuery.unobstrustive-ajax.min.js is not required for this call. Commented Jan 25, 2016 at 17:27

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. After changing failure to error it started working. :)
2

The things I can see right off the bat:

  1. $.ajax doesn't take a "failure" parameter, but rather "error." Even then, you should be using .done() and .fail() instead (see http://api.jquery.com/jQuery.ajax/)
  2. Your error handling function will be triggered when your page method throws an exception. The first parameter is a jqXHR object, not a JSON response from .NET (see the same link as from #1).
  3. Your page method should not go UNDER the page class, but within it.

Comments

0

JQuery has some error

    $("#< %=txtUserName.ClientID%>")[0].value

need to be

    $("#<%=txtUserName.ClientID%>")[0].value

or

    $("#<%=txtUserName.ClientID%>").val() 

1 Comment

Thanks. But the space here is by mistake. In my program it is the same as what you have suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.