Ajax a utilizar JQuery em ASP.NET c#

Estou a tentar usar o JQuery ajax em asp. net c#. O código que estou a usar é ...

FORMULÁRIO HTML:

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

Parte JQuery:

<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>

Parte CS:

    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();
    }

problema: Não está a funcionar. Por favor, avise-me se eu estiver faltando alguma configuração ou qualquer espaço de nome. Ou qualquer coisa. URL I Put in ajax Call is correct as I verified it by var pathname = window.localizacao.pathname; também forneci dados ao ligar para o ajax.

Author: Enigmatic Mind, 2016-01-25

3 answers

Talvez a mudar

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

A

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

Vai ajudar a encontrar o problema. Não vejo uma falha no retorno nos documentos. http://api.jquery.com/jquery.ajax/

 2
Author: majita, 2016-04-17 02:53:47

As coisas que consigo ver logo a seguir:

  1. $.ajax não toma um parâmetro de "falha", mas sim "erro"." Mesmo então, devias estar a consumir .feito () e .fail () instead (see http://api.jquery.com/jQuery.ajax/)
  2. a sua função de tratamento de erros será despoletada quando o seu método de página abre uma excepção. O primeiro parâmetro é um objeto jqXHR, não um JSON response from .NET (see the same link as from #1).
  3. o seu método de página não deve ir Debaixo da classe page, mas dentro dela.
 2
Author: Chris Bergin, 2016-01-25 17:45:01

JQuery tem algum erro

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

Tem de ser

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

Ou

    $("#<%=txtUserName.ClientID%>").val() 
 0
Author: Tony Dong, 2016-01-25 17:53:36