Error webmethod with form

0

I have found why I get an error when calling a webmethod , and it is because of taking a variable of text that is inside a <form runat="server"> . The code of method is as follows:

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

}
}

And the code where I call the webmethod is the following:

  <!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);
   }

When calling it by entering the div within that div that I highlight with asterisks (in the actual code it does not have those asterisks it's just pair to be seen here ) I get Ajax error and if I remove that form , it returns OK correctly, do you know why this can be and how can I make it work having a form ?

    
asked by wuasaa 10.01.2017 в 17:55
source

1 answer

0

I finally found the error. It is by calling the method from a% normal button and not a asp:Button .

    
answered by 10.01.2017 в 18:29