【发布时间】:2012-10-28 05:52:42
【问题描述】:
我正在尝试使用 jquery ajax 在 aspx 页面中调用 webmethod。 ajax 代码在页面中调用,但我无法进入该方法,尽管在 ajax Post 请求之后访问了 Page_Load。我尝试了很多方法,但我做不到。
希望你能帮帮我,我快疯了。
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"
});
更新:
这是我在 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:
解决方案
我为我的具体问题所做的是:
$.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()
}
谢谢大家,我感谢您的建议!
【问题讨论】:
-
你安装了 Fiddler 吗?它将向您展示网络上发生的事情以及 .NET 运行时在您的 web 方法命中之前可能抛出的任何异常文本
-
你想做什么?您没有从 Web 服务返回任何内容,并且在 ajax 调用成功后没有任何操作。
-
您得到什么响应,500 内部服务器错误?
-
嗨,如果我不是很清楚,对不起。我英语说得不太好。实际上我已经使用 Firebug 和 Visual Studio 断点进行了调试。 ajax调用方法后,进入aspx页面并执行pageload,没有显示任何异常但没有进入方法。我说清楚了吗?对不起,我的英语。
标签: c# jquery asp.net ajax webmethod