No AJAX response in aspx

2

I have a method in the codeBehind of a webForm in which I redirect an AJAX query

[WebMethod]
public string buscarUser(string name, string pass)
{
     SqlDataReader rd;
     string resp = "CASA;
     return resp;
}

AJAX

$(document).on('click', '.loginBtn', function () {
        var datos = [];
        datos[0] = $("#tbUser").val();
        datos[1] = $("#tbPass").val();
        var datos={name: datos[0], pass: datos[1]};
        $.ajax({
            url: "Login.aspx/buscarUser",
            method: "POST",
            data:JSON.stringify(datos) ,
            async: true,
            dataType: "json",
            success: function (respuesta) {
                alert(respuesta);

            }
        });
    })

The question is that no matter how hard you try to show the alert(respuesta) (CASA), I feel that you are not even entering the method, which is what fails in the code?

    
asked by Baker1562 17.08.2018 в 23:46
source

1 answer

1

I solved it in the following way

The method must be declared as public and static, which are requirements for the WebMethods.

[WebMethod]
public static string buscarUser(string name, string pass)
{
    return "CASA";
}

AJAX

$(document).on('click', '.loginBtn', function () {

    var datos = {
        name: $("#tbUser").val(),
        pass: $("#tbPass").val()
    };

    $.ajax({
        url: "Login.aspx/buscarUser",
        method: "POST",
        data: JSON.stringify(datos),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (respuesta) {
            alert(respuesta.d);
        },
        error: function (data) {
            alert(data.responseText);
        }
    });
})
    
answered by 18.08.2018 / 03:43
source