0

He encontrado por qué me da un error al llamar un webmethod, y es a causa de coger una variable de un text que esta dentro de un <form runat="server">. El código del method es el siguiente:

    using System.Web.Script.Services;

  namespace PortalClientes
{
/// <summary>
/// Summary description for Funciones
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class Funciones : System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string sendMail(string CIF)
    {


        return "OK";
    }

}
}

Y el código de donde llamo al webmethod es el siguiente:

  <!DOCTYPE html>

  <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title></title>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
   </head>

  <body>   
 ***<form runat="server">***
<div>
    <input type="text" id="CIF" />
    <button id="butEnviarMail">Enviar Mail</button>    
</div>    
 ***</form>***

 <script type="text/javascript">
$("#butEnviarMail").click(function () {

    enviaMail();        
});

function enviaMail() {

    var textcif = $("[id*='CIF']").val();

    $.ajax({
        type: "POST",
        async:true,
        url: "Funciones.asmx/sendMail",
        data: "{'CIF': '" + textcif + "'}",
        cache: true,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: env,
        error: errorAjax
        });
}
   function env(msg) {

           alert(msg.d);

   }
   function errorAjax(xmlHttpRequest, textStatus, errorThrown) {
       alert(xmlHttpRequest.responseText);
       alert(textStatus);
       alert(errorThrown);
   }

Al llamarlo metiendo el div dentro de ese div que remarco con asteriscos(en el código real no tiene esos asteriscos es sólo par que se vea aquí ) me da error Ajax y si quito ese form, me devuelve OK correctamente, sabeis por qué puede ser esto y cómo puedo hacer que funcione teniendo un form?

1
  • quitale el runat="server" al tag de form el 10 ene. 2017 a las 17:16

1 respuesta 1

0

Por fin encontré el error. Es por llamar al método desde un button normal y no un asp:Button.

1
  • de ser este tu error y tu solución podrías marcar tu respuesta como aceptada
    – eezzekl
    el 13 ene. 2017 a las 18:22

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.