How to consume a WebService "asmx" via Jquery Ajax

4

I have created a button that calls the method findCompany which in turn is made a Ajax request to consume a Web Service Asxm .

Why does Ajax not consume the Web Service?

<asp:Button ID="btnPesquisar" runat="server" 
    Text="PesquisarEmpresa" OnClientClick="buscarEmpresa();"/>

The Ajax Jquery function

    function buscarEmpresa() {
        $.ajax({
            type: "POST",
            url: "../../webService/Ajax.asmx/GetEmpresa",
            data: "{'cnpj':'" + $("#inputCnpjEmpresa").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data.Cnpj);
            },
            error: function () {
                alert("Erro");
            }
        });

    }

My Asmx.cs

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 Ajax : System.Web.Services.WebService {

    public Ajax () {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public static Empresa GetEmpresa(string cnpj)
    {
        return EmpresaControler.RetornaEmpresa(cnpj); 
    }
}
    
asked by anonymous 28.09.2014 / 01:41

1 answer

4

From both the search and the tips of @TiagoSilva, I discovered that the annotation

[System.Web.Script.Services.ScriptService] 

It was commented, that's why the script did not consume the webService.

    
28.09.2014 / 03:33