简体   繁体   中英

Jquery Ajax and asp.net WebMethod

I'm trying to call a webmethod in an aspx page using jquery ajax. The ajax code is callind the page but I can't go into the method although the Page_Load is been accesed after the ajax Post request. I've tried in many ways but I can't.

I hope you can help me, I'm going crazy.

    protected void Page_Load(object sender, EventArgs e)
    {
        string nombre = Request.QueryString["nombre"];
        if (!IsPostBack)
        {
            this.CargarDatosIniciales();                  
        }
    }

    [WebMethod(enableSession:true)]
    [ScriptMethod()]
    public static void GuardarDatosFamilia(string nombre, string tipoDoc)
    {
        string nombrePersona = nombre;
        string tipoDocumento = tipoDoc;
    }


    $.ajax({
        type: "POST",
        url: "FRM_Caracterizacion.aspx/GuardarDatosFamilia", //Direccion del servicio web segido de /Nombre del metodo a llamar
        beforeSend: function () { alert('I am sending'); },
        data: "{'nombre':'"+ nombre+"','tipoDoc':'"+ tipoDoc"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json" 
        });

UPDATE:

This is what I get in Firebug:

     POST http://localhost:51620/FRM_Caracterizacion.aspx/GuardarDatosFamilia 200 OK    3.22s

     Parámetros application/x-www-form-urlencoded
     nombre Jhon Fredy
     tipoDoc    1
     Fuente
     nombre=Jhon+Fredy&tipoDoc=1

UPDATE 2:

SOLUTION

What I've done for my specific problem was:

     $.ajax({
        type: "POST",
        url: "FRM_Caracterizacion.aspx", //Direccion del servicio web segido de /Nombre del metodo a llamar
        beforeSend: function () { alert('I am sending'); },
        data: { metodo: 'AgregarDatosFamilia',
        nombre:nombre,
        tipoDoc:tipoDoc
        },
        dataType: "json" //Esto quiere decir que los datos nos llegaran como un objeto json
    });


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Form["metodo"] == "AgregarDatosFamilia")
            {
                this.GuardarDatosFamilia();
            }
            this.CargarDatosIniciales();                  
        }
    }

    public void GuardarDatosFamilia()
    {
        string nombre = Request.Form["nombre"].ToString(),
        string tipoDoc = Request.Form["tipoDoc"].ToString()
    }

Thanks everybody, I appreciate suggestions!

make sure you are properly calling this on client side

  $.ajax({
        type: "POST",
        url: "FRM_Caracterizacion.aspx/GuardarDatosFamilia", //Direccion del servicio web segido de /Nombre del metodo a llamar
        beforeSend: function () { alert('I am sending'); },
        data: "{'nombre':'"+ nombre+"','tipoDoc':'"+ tipoDoc"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json" 
        });

then in the browser hit F12 and watch the traffic - you will see that the webmethod is being called , you are not returning anything though ,

[WebMethod(enableSession:true)]
[ScriptMethod()]  //this can't be void - change to String
public static String GuardarDatosFamilia(string nombre, string tipoDoc)
{
    string nombrePersona = nombre;
    string tipoDocumento = tipoDoc;
    return "successful ajax";
}

try that to test - also if you were trying to access string nombre that was declared in Page_Load - that is not possible in a Static Method , the only data you will have access to is what was passed into webmethod

I put a comment saying to change it from void - it actually can be void - but that is if you want to perform some action , usually with a database - even then its good practice to return a string to let the client know if it was a success or not

为webmethod创建一个不同的web服务,阅读更多Consuming-Webservice-using-JQuery-ASP-NET-Applicat调用webservice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM