Jquery Ajax and asp.net WebMethod

jisazat picture jisazat · Nov 8, 2012 · Viewed 9.4k times · Source

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!

Answer

Scott Selby picture Scott Selby · Nov 8, 2012

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