【问题标题】:Jquery Ajax and asp.net WebMethodJquery Ajax 和 asp.net WebMethod
【发布时间】: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


【解决方案1】:

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

  $.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 并观察流量 - 你会看到 web 方法正在被调用,但你没有返回任何东西,

[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 - 但那是如果你想执行一些操作,通常使用数据库 - 即使这样,返回一个字符串让客户知道也是一个好习惯成功与否

【讨论】:

  • 不知道怎么回事,好像是正确调用了aspx页面,我有一个问题,如果我在webmethod中设置断点必须在调试时安排?非常感谢。
  • 我遇到了与@jisazat 类似的问题。就我而言,它不会闯入该方法,而是返回整页标记。我是否需要特殊的配置设置才能使 ASP.NET 使用这些属性,或者...?
【解决方案2】:

为 webmethod 创建不同的 web 服务,阅读更多 Consuming-Webservice-using-JQuery-ASP-NET-Applicatcalling webservice

【讨论】:

  • 我一直把 webmethods 放在 aspx 页面中
  • 他不想调用 web 服务 - 他想在 aspx 页面中调用 webmethod
猜你喜欢
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
  • 2011-05-28
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 2011-10-23
  • 1970-01-01
相关资源
最近更新 更多