4

我正在尝试使用 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()
    }

谢谢大家,我很感激建议!

4

2 回答 2

2

确保您在客户端正确调用它

  $.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" 
        });

然后在浏览器中按 F12 并观察流量 - 你会看到 webmethod 正在被调用,但你没有返回任何东西,

[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";
}

尝试进行测试-如果您尝试访问在 Page_Load 中声明的字符串 nombre-在静态方法中是不可能的,您将可以访问的唯一数据是传递给 webmethod 的数据

我发表评论说要从 void 更改它 - 它实际上可以是 void - 但那是如果你想执行一些操作,通常使用数据库 - 即使这样,返回一个字符串让客户知道它是否是一个好习惯成功与否

于 2012-11-08T04:10:06.423 回答
1

为 webmethod 创建不同的 webservice,阅读更多Consuming-Webservice-using-JQuery-ASP-NET- Applic 和调用 webservice

于 2012-11-08T04:03:18.430 回答