我想在使用jquery ajax的aspx页面中调用webmethod。 ajax代码是调用页面,但我不能进入该方法,尽管Page_Load在ajax Post请求之后已被接受。我尝试了很多方法,但是我做不到。jquery Ajax和asp.net WebMethod
我希望你能帮助我,我要疯了。
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:
这是我得到在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
更新2:
SOLUTION
我已经为我的具体问题做的是:
$.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()
}
谢谢大家,我欣赏建议!
您是否安装了小提琴手?它会告诉你什么是通过电线和.NET运行时可能在您的webmethod命中之前抛出的任何表述文本 – akatakritos
究竟是什么错误? –
你想做什么?你没有从Web服务中返回任何东西,并且在Ajax调用成功后没有任何事情。 –