2012-11-08 84 views
2

我想在使用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() 
    } 

谢谢大家,我欣赏建议!

+0

您是否安装了小提琴手?它会告诉你什么是通过电线和.NET运行时可能在您的webmethod命中之前抛出的任何表述文本 – akatakritos

+0

究竟是什么错误? –

+0

你想做什么?你没有从Web服务中返回任何东西,并且在Ajax调用成功后没有任何事情。 –

回答

0

确保你正确地在客户端调用这个

$.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 - 在静态方法中无法实现,则唯一可以访问的数据是传入web方法的内容

我把一个评论说它将从无效变为无效 - 实际上它可能是无效的 - 但是,如果你想要执行一些操作,通常是使用数据库 - 即使这样做的好习惯是返回一个字符串,让客户知道它是否成功或不是

+0

我不知道什么是错的,它似乎是正确调用aspx页面,我有一个问题,如果我在webmethod中设置断点,它必须安排在调试时间? 非常感谢。 – jisazat

+1

是的,断点在本地主机上调试时会起作用 –

+0

我和@jisazat有类似的问题。在我的情况下,它不会闯入方法,而是返回整页标记。我是否需要特殊的配置设置才能使ASP.NET使用这些属性,或者...? –