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!